I am trying my hand at making a GUI with tkinter and so far I’ve been successful.
I need to make 4 buttons appear in my window I’ve created and i’ve placed them using .grid()
.
Their column is the same so it’s not a problem but I don’t know how to set their different rows much cleaner.
I have typed this which works but I know it could be done better than manually setting the rows and columns:
JavaScript
x
16
16
1
testbutton = Button(root, text="TEST", command=starttest)
2
testbutton.grid(row=16, column=19)
3
testbutton.config(height=4, width=20)
4
5
resultat1 = Button(root, text="Resultat 1", command=lambda: opentext("1"))
6
resultat1.grid(row=2, column=19)
7
resultat1.config(height=4, width=20)
8
9
resultat2 = Button(root, text="Resultat 2", command=lambda: opentext("2"))
10
resultat2.grid(row=4, column=19)
11
resultat2.config(height=4, width=20)
12
13
resultat3 = Button(root, text="Resultat 3", command=lambda: opentext("3"))
14
resultat3.grid(row=8, column=19)
15
resultat3.config(height=4, width=20)
16
I have taken a different approach like the title says using a for
loop and have gotten to this point:
JavaScript
1
21
21
1
def start():
2
buttons = []
3
for i in range(4):
4
button = Button(root, command=lambda i=i: printtest(i))
5
button.grid(column=19)
6
button.config(height=4, width=20)
7
8
def printtest(i):
9
print("test ok")
10
start()
11
12
col_count, row_count = root.grid_size()
13
14
for col in range(col_count):
15
root.grid_columnconfigure(col, minsize=31)
16
17
for row in range(row_count):
18
root.grid_rowconfigure(row, minsize=31)
19
20
root.mainloop()
21
Is what I want to do is possible and if it can, how can I do it?
Advertisement
Answer
I do something like this
JavaScript
1
101
101
1
#!/usr/bin/python3
2
import sys
3
import tkinter as tk
4
from tkinter import ttk
5
from tkinter import messagebox
6
7
8
class App(tk.Tk):
9
"""Application start here"""
10
def __init__(self):
11
super().__init__()
12
13
self.protocol("WM_DELETE_WINDOW", self.on_close)
14
self.resizable(width=False, height=False)
15
self.title("Simple App")
16
self.text = tk.StringVar()
17
self.init_ui()
18
19
20
def cols_configure(self, w):
21
22
w.columnconfigure(0, weight=0, minsize=80)
23
w.columnconfigure(1, weight=0)
24
25
w.rowconfigure(0, weight=0, minsize=50)
26
w.rowconfigure(1, weight=0,)
27
28
def init_ui(self):
29
30
w = ttk.Frame(self, padding=8)
31
self.cols_configure(w)
32
33
r = 0
34
c = 1
35
ttk.Label(w, text="Resultat 1").grid(row=r, sticky=tk.W)
36
self.Resultat1 = ttk.Entry(w, justify=tk.CENTER,)
37
self.Resultat1.grid(row=r,
38
column=c,
39
sticky=tk.W,
40
padx=5, pady=5)
41
42
r +=1
43
ttk.Label(w, text="Resultat 2").grid(row=r, sticky=tk.W)
44
self.Resultat2 = ttk.Entry(w, justify=tk.CENTER,)
45
self.Resultat2.grid(row=r,
46
column=c,
47
sticky=tk.W,
48
padx=5, pady=5)
49
50
51
r +=1
52
ttk.Label(w, text="Resultat 3").grid(row=r, sticky=tk.W)
53
self.Resultat3 = ttk.Entry(w, justify=tk.CENTER,)
54
self.Resultat3.grid(row=r,
55
column=c,
56
sticky=tk.W,
57
padx=5, pady=5)
58
59
60
r = 0
61
c = 2
62
63
b = ttk.LabelFrame(self, text="", relief=tk.GROOVE, padding=5)
64
65
bts = [("TEST", 0, self.on_callback, "<Alt-t>"),
66
("Resultat 1", 1, self.on_callback, "<Alt-r>"),
67
("Resultat 2", 2, self.on_callback, "<Alt-e>"),
68
("Resultat 3", 4, self.on_callback, "<Alt-u>"),
69
("Close", 0, self.on_close, "<Alt-c>")]
70
71
for btn in bts:
72
ttk.Button(b, text=btn[0], underline=btn[1], command = btn[2]).grid(row=r,
73
column=c,
74
sticky=tk.N+tk.W+tk.E,
75
padx=5, pady=5)
76
self.bind(btn[3], btn[2])
77
r += 1
78
79
b.grid(row=0, column=1, sticky=tk.N+tk.W+tk.S+tk.E)
80
w.grid(row=0, column=0, sticky=tk.N+tk.W+tk.S+tk.E)
81
82
83
def on_callback(self, evt=None):
84
85
print("on_callback")
86
87
88
def on_close(self,evt=None):
89
"""Close all"""
90
if messagebox.askokcancel(self.title(), "Do you want to quit?", parent=self):
91
self.destroy()
92
93
def main():
94
95
app = App()
96
app.mainloop()
97
98
if __name__ == '__main__':
99
main()
100
101