Python 7 Levels, L7, Unit 77, Event Handling

Powered By EmbedPress

Exercise 1:

import tkinter as tk
import tkinter.messagebox

class Application:

    def __init__(self):
        self.root = tk.Tk()
        self.root.title("Event Handling")
        self.root.geometry("300x200+100+100")
        self.createWidget()

    def createWidget(self):
        self.btn = tk.Button(self.root,text="Hello",command=lambda:self.printHello("David Smith"))
        self.btn.pack()

    def printHello(self,name):
        tk.messagebox.showinfo("Test","Hello, "+name)

    def run(self):
        self.root.mainloop()

app = Application()
app.run()

Exercise 2:

import tkinter as tk

class Application:

    def __init__(self):
        self.root = tk.Tk()
        self.root.title("Mouse Event Test")
        self.root.geometry("300x200+100+100")
        self.createWidget()

    def createWidget(self):
        self.btn = tk.Button(self.root,text="Test")
        self.btn.pack()
        self.btn.bind("<ButtonPress-1>",self.buttonPress)
        self.btn.bind("<ButtonRelease-1>",self.buttonRelease)
        
    def buttonPress(self,event):
        print("Left button clicked.")

    def buttonRelease(self,event):
        print("Left Button released")

    def run(self):
        self.root.mainloop()

app = Application()
app.run()

Exercise 3:

import tkinter as tk

class Application:

    def __init__(self):
        self.root = tk.Tk()
        self.root.title("Mouse Event Test")
        self.root.geometry("300x200+100+100")
        self.root.bind("<KeyPress>",self.keyPress)
        self.createWidget()

    def createWidget(self):
        self.label = tk.Label(self.root,text="Test",width=5,height=5,bg="yellow")
        self.label.pack()
        self.label.bind("<B1-Motion>",self.b1_motion)
        self.label.bind("<ButtonPress>",self.buttonPress)

    def b1_motion(self,event):
        print("Coordinates of mouse:(Relative to parent container){0},{1}".format(event.x,event.y))
        print("Coordinates of mouse:(Relative to screen){0},{1}".format(event.x_root,event.y_root))

    def buttonPress(self,event):
        print("Mouse button no.:",event.num)

    def keyPress(self,event):
        print("Character of keyboard key:{0},Code:{1},Name:{2}"\
              .format(event.char,event.keycode,event.keysym))

    def run(self):
        self.root.mainloop()

app = Application()
app.run()

Exercise 4:

import tkinter as tk
import tkinter.messagebox

class Application:

    def __init__(self):
        self.root = tk.Tk()
        self.root.title("protocol test")
        self.root.geometry("300x200+100+100")
        self.root.protocol("WM_DELETE_WINDOW",self.exit)

    def exit(self):
        if tk.messagebox.askyesno("Close Window","Are you sure to close window?"):
            self.root.destroy()

    def run(self):
        self.root.mainloop()

app = Application()
app.run()

Homework

import tkinter as tk
import tkinter.messagebox

class Application:

    def __init__(self):
        self.root = tk.Tk()
        self.root.title("True Like Test")
        self.root.geometry("200x100+100+100")
        self.root.protocol("WM_DELETE_WINDOW",self.close)
        self.createWidget()

    def createWidget(self):
        self.label = tk.Label(self.root,text="Do you like me?")
        self.label.pack()
        self.frame = tk.Frame(self.root)
        self.frame.pack(pady=10)
        self.btn_01 = tk.Button(self.frame,text = "Like")
        self.btn_01.pack(side="left")
        self.btn_01.bind("<ButtonPress-1>",self.btn01_press)
        self.btn_02 = tk.Button(self.frame,text = "Not Like",command=self.btn02_press)
        self.btn_02.pack(side="left")
        
    def btn01_press(self,event):
        tk.messagebox.showinfo("True Like Test","Haha,I like you too.")
        self.root.destroy()

    def btn02_press(self):
        tk.messagebox.showinfo("True Like Test","Wrong,Pick one more time.")

    def close(self):
         tk.messagebox.showinfo("True Like Test","You can not close before picking")

    def run(self):
        self.root.mainloop()

app = Application()
app.run()

dahan1999

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts