Python 7 Levels, L7, Unit 73, Glance at tkinter

Powered By EmbedPress

Exercise 1:

import tkinter as tk

root = tk.Tk()#create a main windows
root.title("test")
root.geometry("400x300+200+100")

def hello():
    tk.messagebox.showinfo("test","hello")

#add a label
label_01 = tk.Label(root,text = "Label")
label_01.pack()

#add a button
btn_01 = tk.Button(root,text = "Button",command = hello)
btn_01.pack()

#add text input
entry_01 = tk.Entry(root)
entry_01.pack()

root.mainloop()

Exercise 2:

import tkinter as tk

class Application:

    def __init__(self):
        self.root = tk.Tk()
        self.root.title("test")
        self.root.geometry("400x300+200+100")
        self.createWidget()

    def createWidget(self):
        #add label
        self.label_01 = tk.Label(self.root,text = "Label")
        self.label_01.pack()

        #add button
        self.btn_01 = tk.Button(self.root,text = "Button",command = self.hello)
        self.btn_01.pack()

        #add text input
        self.entry_01 = tk.Entry(self.root)
        self.entry_01.pack()

    def hello(self):
        tk.messagebox.showinfo("test","hello")

    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("Login")
        self.root.geometry("200x140+100+100")
        self.createWidget()

    def createWidget(self):
        self.label_01 = tk.Label(self.root,text="User Name")
        self.label_01.pack()

        self.entry_01 = tk.Entry(self.root)
        self.entry_01.pack()

        self.label_02 = tk.Label(self.root,text="Password")
        self.label_02.pack()

        self.entry_02 = tk.Entry(self.root)
        self.entry_02.pack()

        self.btn = tk.Button(self.root,text="Login",command = self.done)
        self.btn.pack()

    def done(self):
        tk.messagebox.showinfo("Login Status","Login Succeeds.")

    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