From d0c9195cfd29a8a6cc26ad372068d600a7a307ea Mon Sep 17 00:00:00 2001 From: Martin Putzlocher Date: Tue, 28 Dec 2021 13:28:53 +0100 Subject: [PATCH] Auslosung und Progressbar --- auslosung.py | 18 ++++++++++ tk_first_steps/events.py | 32 +++++++++++++++++ tk_progress/my_progress.py | 71 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 121 insertions(+) create mode 100644 auslosung.py create mode 100644 tk_first_steps/events.py create mode 100644 tk_progress/my_progress.py diff --git a/auslosung.py b/auslosung.py new file mode 100644 index 0000000..0a98ba4 --- /dev/null +++ b/auslosung.py @@ -0,0 +1,18 @@ +from random import shuffle + +def print_auslosung(title, studdict): + studentslist = list(range(len(studdict))) + shuffle(studentslist) + print("== {} ==".format(title)) + i = 1 + for num in studentslist: + print("{} - {}".format(i, studdict[num+1])) + i += 1 + print("---") + return True + +studentsdict1 = {1: "Simon", 2: "Maximilian", 3: "Hannah", 4: "Laura", 5: "Clara"} +studentsdict2 = {1: "Vanessa", 2: "Pascal", 3: "Oliver"} + +print_auslosung("17.12.2021", studentsdict1) +print_auslosung("21.12.2021", studentsdict2) diff --git a/tk_first_steps/events.py b/tk_first_steps/events.py new file mode 100644 index 0000000..52ba76d --- /dev/null +++ b/tk_first_steps/events.py @@ -0,0 +1,32 @@ +import tkinter +from tkinter import * + +ws = Tk( ) +ws.geometry("200x200") +display='Press Any Button, or Press Key' +Lab= Label(ws, text=display, width=len(display)) +Lab.pack(pady=40) + +def key(eve): + if eve.char==eve.keysym: + message ='Normal Key %r' % eve.char + elif len(eve.char)==1: + message ='Punctuation Key %r (%r)' % (eve.keysym, eve.char) + else: + message ='Special Key %r' % eve.keysym + Lab.config(text=message) +Lab.bind_all('', key) + +def do_mouse(eventname): + def mouse_binding(event): + message = 'Mouse event %s' % eventname + Lab.config(text=message) + Lab.bind_all('<%s>'%eventname, mouse_binding) + +for i in range(1,4): + do_mouse('Button-%s'%i) + do_mouse('ButtonRelease-%s'%i) + do_mouse('Double-Button-%s'%i) + + +ws.mainloop() diff --git a/tk_progress/my_progress.py b/tk_progress/my_progress.py new file mode 100644 index 0000000..b3cbd36 --- /dev/null +++ b/tk_progress/my_progress.py @@ -0,0 +1,71 @@ +import tkinter as tk +import tkinter.ttk as ttk + +class Controller(object): + def __init__(self, app): + self.app = app + self.count = 0 + self.running = False + self.interrupt = False + + def run_process(self): + steps = 42 + delta = 100 / steps + + print("run {}".format(self.count)) + self.set_running() + + if self.count == steps or self.interrupt: + print(self.count) + self.running = False + self.finish() + return 0 + else: + self.count += 1 + self.app.after(1, lambda : self.app.p.step(delta)) + self.app.after(50, self.run_process) + return 1 + + def set_interrupt(self): + self.interrupt = True + self.app.b.configure(text="Reset", command=self.set_reset) + + def set_running(self): + if not self.interrupt: + self.running = True + self.app.b.configure(text="Abort", command=self.set_interrupt) + + def set_reset(self): + self.count = 0 + self.interrupt=False + self.app.pbar_var.set(0) + self.app.b.configure(text="Start", command=self.run_process) + + def finish(self): + print("finished.") + self.app.b.configure(text="Restart", command=self.set_reset) + + +class Application(tk.Tk): + def __init__(self): + super().__init__() + self.con = Controller(self) + self.create_control_variables() + self.create_widgets() + + def create_control_variables(self): + self.pbar_var = tk.IntVar() + + def create_widgets(self): + self.p = ttk.Progressbar(master=self, length="200", maximum=100, mode="determinate", variable=self.pbar_var) + self.p.grid(row=0, column=0) + + self.b = ttk.Button(master=self, text="Start", command=self.con.run_process) + self.b.grid(row=1, column=0) + +# -------------------------- +# Execution +# -------------------------- +if __name__ == "__main__": + app = Application() + app.mainloop()