Simple windowed application: Difference between revisions

Content added Content deleted
m (→‎{{header|Python}}: fixed libheaders)
(→‎{{libheader|Tkinter}}: Applied best Python practices to both versions)
Line 2,211: Line 2,211:


==={{libheader|Tkinter}}===
==={{libheader|Tkinter}}===
<lang python>from Tkinter import Tk, Label, Button
<lang python>from functools import partial


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()
label["text"] = f"Number of clicks: {counter.get()}"
Button(w, text="click me", command=update_label).pack()

w.mainloop()</lang>

def main():
window = tk.Tk()
label = tk.Label(master=window,
text="There have been no clicks yet")
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()
window.mainloop()


if __name__ == '__main__':
main()
</lang>
The same in OO manner:
The same in OO manner:
<lang python>#!/usr/bin/env python
<lang python>import tkinter as tk
from Tkinter import Button, Frame, Label, Pack



class ClickCounter(Frame):
class ClickCounter(tk.Frame):
def click(self):
self.count += 1
def __init__(self, master=None):
super().__init__(master)
self.label['text'] = 'Number of clicks: %d' % self.count
tk.Pack.config(self)
self.label = tk.Label(self, text='There have been no clicks yet')
def createWidgets(self):
self.label = Label(self, text='here have been no clicks yet')
self.label.pack()
self.label.pack()
self.button = Button(self, text='click me', command=self.click)
self.button = tk.Button(self,
text='click me',
command=self.click)
self.button.pack()
self.button.pack()
def __init__(self, master=None):
Frame.__init__(self, master)
Pack.config(self)
self.createWidgets()
self.count = 0
self.count = 0

def click(self):
if __name__=="__main__":
self.count += 1
ClickCounter().mainloop()</lang>
self.label['text'] = f'Number of clicks: {self.count}'


if __name__ == "__main__":
ClickCounter().mainloop()

</lang>


==={{libheader|PyQt}}===
==={{libheader|PyQt}}===