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 |
<lang python>from functools import partial |
||
import tkinter as tk |
|||
def update_label(): |
|||
global n |
|||
n += 1 |
|||
⚫ | |||
w = Tk() |
|||
def on_click(label: tk.Label, |
|||
n = 0 |
|||
counter: tk.IntVar) -> None: |
|||
⚫ | |||
counter.set(counter.get() + 1) |
|||
⚫ | |||
⚫ | |||
Button(w, text="click me", command=update_label).pack() |
|||
w.mainloop()</lang> |
|||
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, |
|||
⚫ | |||
command=update_counter) |
|||
button.pack() |
|||
⚫ | |||
⚫ | |||
main() |
|||
</lang> |
|||
The same in OO manner: |
The same in OO manner: |
||
<lang python> |
<lang python>import tkinter as tk |
||
from Tkinter import Button, Frame, Label, Pack |
|||
class ClickCounter(Frame): |
class ClickCounter(tk.Frame): |
||
⚫ | |||
def __init__(self, master=None): |
|||
⚫ | |||
⚫ | |||
tk.Pack.config(self) |
|||
⚫ | |||
def createWidgets(self): |
|||
⚫ | |||
self.label.pack() |
self.label.pack() |
||
self.button = Button(self, |
self.button = tk.Button(self, |
||
text='click me', |
|||
command=self.click) |
|||
self.button.pack() |
self.button.pack() |
||
def __init__(self, master=None): |
|||
⚫ | |||
Pack.config(self) |
|||
⚫ | |||
self.count = 0 |
self.count = 0 |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
if __name__ == "__main__": |
|||
ClickCounter().mainloop() |
|||
</lang> |
|||
==={{libheader|PyQt}}=== |
==={{libheader|PyQt}}=== |