Code: Alles auswählen
def read(self, event):
text = self.lb.get(lb.curselection())
print text
(Ich würd' dir ja jetzt'n funktionierendes Beispiel geben, aber von Tkinter hab ich recht wenig Ahnung)
Code: Alles auswählen
def read(self, event):
text = self.lb.get(lb.curselection())
print text
Code: Alles auswählen
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python25\lib\lib-tk\Tkinter.py", line 1403, in __call__
return self.func(*args)
TypeError: read() takes exactly 2 arguments (1 given)
Code: Alles auswählen
import Tkinter as tk
class test_GUI:
def __init__(self):
fenster = tk.Tk()
fenster.geometry("110x75")
#Die Listbox
self.lb = tk.Listbox(fenster, selectmode=tk.EXTENDED)
self.lb.pack()
self.lb.place(x=18, y=10, width=80, height=35)
self.lb.insert(tk.END, "Peter")
self.lb.insert(tk.END, "Hans")
#Der Button
test_button = tk.Button(fenster, text="test", command=self.read)
test_button.place(x=6, y=50, width=100, height=20)
#die Methode
def read(self):
for i in self.lb.curselection():
text = self.lb.get(i)
print text
__name__ == '__main__'
dasFenster = test_GUI()
tk.mainloop()
Code: Alles auswählen
import Tkinter as tk
class TestGUI:
def __init__(self):
fenster = tk.Tk()
self.listbox = tk.Listbox(fenster,
selectmode=tk.EXTENDED,
width=10,
height=2)
for name in ('Peter', 'Hans'):
self.listbox.insert(tk.END, name)
self.listbox.pack()
test_button = tk.Button(fenster, text='test', command=self.read)
test_button.pack()
def read(self):
print self.listbox.get(self.listbox.curselection())
if __name__ == '__main__':
das_fenster = TestGUI()
tk.mainloop()
Code: Alles auswählen
tk.Listbox(hp, selectmode=tk.MULTIPLE)
Code: Alles auswählen
tk.Listbox(hp, selectmode=tk.MULTIPLE, exportselection=0)
Code: Alles auswählen
self.listbox.selection_clear(self.listbox.curselection())
Code: Alles auswählen
File "C:gui.py", line 707, in neu
self.listbox.selection_clear(self.listbox.curselection())
File "C:\Python25\lib\lib-tk\Tkinter.py", line 2530, in selection_clear
'selection', 'clear', first, last)
TclError: bad listbox index "": must be active, anchor, end, @x,y, or a number
Code: Alles auswählen
self.listbox.selection_clear(0,x)
Code: Alles auswählen
global klick
klick=0
def ausgabe():
global klick
klick+=1
if klick % 2 != 0:
global textfenster
textfenster= Text(fenster,width=29,height=10)
textfenster.grid(row=1,columnspan=2)
else:
textfenster.grid_forget()
from Tkinter import*
fenster = Tk()
entry=Entry(fenster)
entry.grid(row=0)
fenster.button = Button(fenster,text ="klick mich", command=ausgabe)
fenster.button.grid(row=0,column=1)
# Fenster anzeigen
fenster.mainloop()
Code: Alles auswählen
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# For Python3.x
import tkinter as tk
class TestGUI:
def __init__(self):
fenster = tk.Tk()
self.mylist = {0 : 'Hans', 1 : 'Peter', 2 : 'Thomas'}
self.listbox = tk.Listbox(fenster,
selectmode=tk.EXTENDED,
width=10,
height=len(self.mylist))
for name in sorted(self.mylist):
self.listbox.insert(tk.END, self.mylist[name])
self.listbox.bind('<<ListboxSelect>>', self.check_select)
self.listbox.pack()
def check_select(self, event):
"""Überprüfe das Ereignis und führe den Auftrag aus"""
indices = list(map(int, self.listbox.curselection()))
result = ''.join([self.mylist[row]
for row in sorted(self.mylist) if row == indices[0]])
print(result)
return result
if __name__ == '__main__':
das_fenster = TestGUI()
tk.mainloop()
Code: Alles auswählen
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# For Python3.x
import tkinter as tk
class TestGUI:
def __init__(self):
fenster = tk.Tk()
self.entry=tk.Entry(fenster)
self.entry.pack()
self.mylist = {0 : 'Hans', 1 : 'Peter', 2 : 'Thomas'}
self.listbox = tk.Listbox(fenster,
selectmode=tk.EXTENDED,
width=10,
height=len(self.mylist))
for name in sorted(self.mylist):
self.listbox.insert(tk.END, self.mylist[name])
self.listbox.bind('<<ListboxSelect>>', self.check_select)
self.listbox.pack()
def check_select(self, event):
"""Überprüfe das Ereignis und führe den Auftrag aus"""
indices = list(map(int, self.listbox.curselection()))
result = ''.join([self.mylist[row]
for row in sorted(self.mylist) if row == indices[0]])
self.entry.delete(0, tk.END)
return self.entry.insert(tk.END, result)
if __name__ == '__main__':
das_fenster = TestGUI()
tk.mainloop()