Simple windowed application: Difference between revisions
→{{libheader|Tkinter}}: Applied best Python practices to both versions
m (→{{header|Python}}: fixed libheaders) |
(→{{libheader|Tkinter}}: Applied best Python practices to both versions) |
||
Line 2,211:
==={{libheader|Tkinter}}===
<lang python>from
import tkinter as tk
l["text"] = "Number of clicks: %d" % n▼
def on_click(label: tk.Label,
counter: tk.IntVar) -> None:
l = Label(w, text="There have been no clicks yet")▼
counter.set(counter.get() + 1)
l.pack()▼
def main():
window = tk.Tk()
label = tk.Label(master=window,
counter = tk.IntVar()
update_counter = partial(on_click,
label=label,
counter=counter)
button = tk.Button(master=window,
text="click me",▼
command=update_counter)
button.pack()
main()
</lang>
The same in OO manner:
<lang python>
class ClickCounter(tk.Frame):
def click(self):▼
def
self.label['text'] = 'Number of clicks: %d' % self.count▼
tk.Pack.config(self)
▲ self.label = Label(self, text='here have been no clicks yet')
self.label.pack()
self.button = tk.Button(self,
text='click me',
command=self.click)
self.button.pack()
▲ Frame.__init__(self, master)
self.createWidgets()▼
self.count = 0
▲ def click(self):
▲if __name__=="__main__":
▲ ClickCounter().mainloop()</lang>
if __name__ == "__main__":
ClickCounter().mainloop()
</lang>
==={{libheader|PyQt}}===
|