Python 7 Levels, L7, Unit 74, tkinter Common Components

Powered By EmbedPress

Exercise 1:

import tkinter as tk

class Application:

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

    def createWidget(self):
        self.value = tk.StringVar()
        self.value.set("Dynamic Label")
        self.label_01 = tk.Label(self.root,textvariable=self.value,\
                                 width=8,height=4,fg="red",bg="yellow",\
                                 font=("Times New Roman",16),justify="left")
        self.value.set("Dynamic Label 2")
        self.label_01.pack()

        global photo
        photo = tk.PhotoImage(file="cktn.gif")
        self.label_02 = tk.Label(self.root,image=photo)
        self.label_02.pack()

    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("Test")
        self.root.geometry("400x300+200+100")
        self.createWidget()

    def createWidget(self):
        self.btn_01 = tk.Button(self.root,text="Button",width=5,\
                                height = 5,fg="red",bg="yellow")
        self.btn_01.pack()

    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("Test")
        self.root.geometry("400x300+200+100")
        self.createWidget()

    def createWidget(self):
        self.v1 = tk.StringVar()
        self.v1.set("abc")
        self.entry_01 = tk.Entry(self.root,textvariable=self.v1)
        self.entry_01.pack()

        self.btn = tk.Button(self.root,text = "Confirm",command = self.printValue)
        self.btn.pack()

    def printValue(self):
        tk.messagebox.showinfo("Return Value",self.v1.get())
        

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

app = Application()
app.run()

Exercise 4:

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):
        self.text_01 = tk.Text(self.root,width = 20,height=4,\
                               fg="red",bg="yellow")
        self.text_01.insert(1.0,"abcdefg\nhhhh")
        self.text_01.insert(2.2,"12345")
        self.text_01.pack()

        self.btn = tk.Button(self.root,text = "Returned Text",command = self.return_text)
        self.btn.pack()

    def return_text(self):
        tk.messagebox.showinfo("Returned Text",self.text_01.get(1.0,2.2))
        

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

app = Application()
app.run()

Exercise 5:

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):
        self.v = tk.StringVar()
        self.r1 = tk.Radiobutton(self.root,text="Male",value ="M",variable=self.v)
        self.r2 = tk.Radiobutton(self.root,text="Female",value ="F",variable=self.v)
        self.v.set("F")
        self.r1.pack(side = "left")
        self.r2.pack(side = "left")
        

    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.v1 = tk.StringVar()
        self.entry_01 = tk.Entry(self.root,textvariable = self.v1)
        self.entry_01.pack()

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

        self.v2 = tk.StringVar()
        self.entry_02 = tk.Entry(self.root,textvariable = self.v2)
        self.entry_02.pack()

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

    def done(self):
        if self.v1.get() == "admin" and self.v2.get() == "123":
            tk.messagebox.showinfo("Login Status","Login Succeeds.")
        else:
            tk.messagebox.showinfo("Login Status","Login Fails")

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

app = Application()
app.run()

dahan1999

Leave a Reply

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

Related Posts