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 Tkinterfunctools import Tk, Label, Buttonpartial
 
import tkinter as tk
def update_label():
global n
n += 1
l["text"] = "Number of clicks: %d" % n
 
 
w = Tk()
def on_click(label: tk.Label,
n = 0
counter: tk.IntVar) -> None:
l = Label(w, text="There have been no clicks yet")
counter.set(counter.get() + 1)
l.pack()
llabel["text"] = f"Number of clicks: %d{counter.get()}" % n
Button(w, text="click me", command=update_label).pack()
 
w.mainloop()</lang>
 
def main():
window = tk.Tk()
label = tk.Label(master=window,
l = Label(w, text="There have been no clicks yet")
l label.pack()
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()
ClickCounter()window.mainloop()</lang>
 
 
if __name__ ==" '__main__"':
main()
</lang>
The same in OO manner:
<lang python>#!/usr/bin/envimport pythontkinter as tk
from Tkinter import Button, Frame, Label, Pack
 
 
class ClickCounter(tk.Frame):
def click(self):
def __init__(self.count, +master= 1None):
Framesuper().__init__(self, master)
self.label['text'] = 'Number of clicks: %d' % self.count
tk.Pack.config(self)
self.label = tk.Label(self, text='hereThere have been no clicks yet')
def createWidgets(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)
text='click me',
command=self.click)
self.button.pack()
def __init__(self, master=None):
Frame.__init__(self, master)
Pack.config(self)
self.createWidgets()
self.count = 0
 
def click(self):
if __name__=="__main__":
self.createWidgets()count += 1
ClickCounter().mainloop()</lang>
self.label['text'] = f'Number of clicks: %d' % {self.count}'
 
 
if __name__ == "__main__":
ClickCounter().mainloop()
 
</lang>
 
==={{libheader|PyQt}}===
35

edits