Seite 1 von 1
Anordnung im Tk
Verfasst: Mittwoch 19. August 2009, 09:37
von Mätthi
Hallo
ich habe folgendes Problem:
Code: Alles auswählen
from Tkinter import Tk, Button
root=Tk()
b1=Button(root,text="(0,0)")
b2=Button(root,text="(1,0)")
b3=Button(root,text="(2,0)")
b4=Button(root,text="(3,0)")
b5=Button(root,text="(0,1)")
b6=Button(root,text="(1,1)")
b7=Button(root,text="(2,1)")
b8=Button(root,text="(3,1)")
b9=Button(root,text="(0,2)")
b10=Button(root,text="(1,2)")
b11=Button(root,text="(2,2)")
b12=Button(root,text="(3,2)")
b13=Button(root,text="(0,3)")
b14=Button(root,text="(1,3)")
b15=Button(root,text="(2,3)")
b16=Button(root,text="(3,3)")
Wie muss ich die Funktion pack() aufrufen,
damit die Knöpfe am richtigen Ort erscheinen
((0,0):oben links; (3,0):oben rechts; (0,3):unten links ect.)?
Danke schon mal im Vorraus.
Verfasst: Mittwoch 19. August 2009, 11:08
von dahaze
Hallo!
Mein Vorschlag:
Benutze lieber die grid-Funktion von Tkinter.
Code: Alles auswählen
import Tkinter
root=Tkinter.Tk()
root.grid()
b1=Tkinter.Button(root,text="(0,0)")
b1.grid(row=0,colum=0)
b2=Tkinter.Button(root,text="(1,0)")
b2.grid(row=1,colum=0)
b3=Tkinter.Button(root,text="(2,0)")
b3.grid(row=2,colum=0)
b4=Tkinter.Button(root,text="(3,0)")
b4.grid(row=3,colum=0)
b5=Tkinter.Button(root,text="(0,1)")
b5.grid(row=0,colum=1)
b6=Tkinter.Button(root,text="(1,1)")
b6.grid(row=1,colum=1)
b7=Tkinter.Button(root,text="(2,1)")
b7.grid(row=2,colum=1)
b8=Tkinter.Button(root,text="(3,1)")
b8.grid(row=3,colum=1)
b9=Tkinter.Button(root,text="(0,2)")
b9.grid(row=0,colum=2)
b10=Tkinter.Button(root,text="(1,2)")
b10.grid(row=1,colum=2)
b11=Tkinter.Button(root,text="(2,2)")
b11.grid(row=2,colum=2)
b12=Tkinter.Button(root,text="(3,2)")
b12.grid(row=3,colum=2)
b13=Tkinter.Button(root,text="(0,3)")
b13.grid(row=0,colum=3)
b14=Tkinter.Button(root,text="(1,3)")
b14.grid(row=1,colum=3)
b15=Tkinter.Button(root,text="(2,3)")
b15.grid(row=2,colum=3)
b16=Tkinter.Button(root,text="(3,3)")
b16.grid(row=3,colum=3)
Tkinter.mainloop()
Gruß,
Simon
Edit: Sorry, hab die Anordnung der Spalten und Zeilen gegenüber deinem gewünschten Ergebnis vertauscht, aber das Prinzip sollte klar sein.
Verfasst: Mittwoch 19. August 2009, 11:24
von EyDu
Wenn man Namen durchnummeriert sollte man Listen, Tupel oder ein Dictionary verwenden. Das ganze erstellen der Buttons könnte man auch in eine Schleife packen. Das sind dann insgesamt 4 Zeilen.
Verfasst: Mittwoch 19. August 2009, 16:45
von Mätthi
Danke

Verfasst: Mittwoch 19. August 2009, 20:56
von Pascal
EyDu hat geschrieben:Wenn man Namen durchnummeriert sollte man Listen, Tupel oder ein Dictionary verwenden. Das ganze erstellen der Buttons könnte man auch in eine Schleife packen. Das sind dann insgesamt 4 Zeilen.
Code: Alles auswählen
>>> for i in range(4):
for e in range(4):
print e, i
0 0
1 0
2 0
3 0
0 1
1 1
2 1
3 1
0 2
1 2
2 2
3 2
0 3
1 3
2 3
3 3

Verfasst: Mittwoch 19. August 2009, 21:47
von EyDu
Und damit willst du jetzt was sagen?
Verfasst: Mittwoch 19. August 2009, 22:20
von wuf
Hallo Mätthi
Hier noch etwas zum ausprobieren. Wurde schon von EyDu angedeutet:
Code: Alles auswählen
import Tkinter as tk
root=tk.Tk()
for row in xrange(4):
for column in xrange(4):
button=tk.Button(root, text="("+str(column)+","+str(row)+")")
button.grid(row=row, column=column)
tk.mainloop()
Gruss wuf

Verfasst: Mittwoch 19. August 2009, 22:23
von kaytec
Hallo Mätthi !
Wuf war schneller - ich hab auch was:
Code: Alles auswählen
import Tkinter as tk
from functools import partial
ROW = 5
COLUMN = 5
def show_grid_position(row, column):
print row, column
root = tk.Tk()
for row in xrange(ROW):
for column in xrange(COLUMN):
tk.Button(text="%s,%s" %(row, column),
command=partial(show_grid_position, row, column)
).grid(row=row, column=column)
root.resizable(0, 0)
root.mainloop()
Gruß Frank
Verfasst: Mittwoch 19. August 2009, 22:24
von EyDu
oder
Code: Alles auswählen
button = tk.Button(root, text="(%d, %d)"%(column, row))
Verfasst: Donnerstag 20. August 2009, 20:16
von Pascal
EyDu hat geschrieben:Und damit willst du jetzt was sagen?
das was wuf gemacht hat ^^