Python 7 Levels, L7, Unit 75, Layout Manager

Powered By EmbedPress

Exercise 1:

import tkinter as tk

class Application:

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

    def createWidget(self):
        self.btn_01 = tk.Button(self.root,text="button1")
        self.btn_01.pack(ipadx=50,ipady=50)

        self.btn_02 = tk.Button(self.btn_01,text="button2")
        self.btn_02.pack(anchor="w")

        self.btn_03 = tk.Button(self.btn_01,text="Button3")
        self.btn_03.pack(anchor="w")
        
    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("Piano Keyboard")
        self.root.geometry("700x220+100+100")
        self.createWidget()

    def createWidget(self):
        self.frame_01 = tk.Frame(self.root)
        self.frame_01.pack()
        self.frame_02 = tk.Frame(self.root)
        self.frame_02.pack()

        textList = ["Light Music","Chinese Style","Euro-Ame","Heavy Metal","Popular"]
        for t in textList:
            self.btn_style = tk.Button(self.frame_01,text=t)
            self.btn_style.pack(side = "left",padx=10)

        for i in range(16):
            if i%2== 0:
                color = "white"
            else:
                color = "black"
            self.btn = tk.Button(self.frame_02,width=5,height=10,bg=color,fg='red')
            self.btn.pack(side = "left",padx=1)
        
    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("Login")
        self.root.geometry("500x100+100+100")
        self.createWidget()

    def createWidget(self):
        self.frame = tk.Frame(self.root)
        self.frame.pack()
        self.label_user = tk.Label(self.frame,text="User Name")
        self.label_user.grid(row=0,column=0)
        self.entry_user = tk.Entry(self.frame)
        self.entry_user.grid(row=0,column=1)
        self.label_01 = tk.Label(self.frame,text="Phone no or email")
        self.label_01.grid(row=0,column=2)

        self.label_psw = tk.Label(self.frame,text="Password")
        self.label_psw.grid(row=1,column=0)
        self.entry_psw = tk.Entry(self.frame,show="*")
        self.entry_psw.grid(row=1,column=1)
        self.label_02 = tk.Label(self.frame,text="Letters combined with digits")
        self.label_02.grid(row=1,column=2)

        self.btn_01 = tk.Button(self.frame,text="Login")
        self.btn_01.grid(row=2,column=0,columnspan=2,sticky="we")
        self.btn_02 = tk.Button(self.frame,text="Cancel")
        self.btn_02.grid(row=2,column=2)
                
    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("place manager")
        self.root.geometry("400x300+200+100")
        self.createWidget()

    def createWidget(self):
        self.frame= tk.Frame(self.root,width=200,height=200,bg="red")
        self.frame.place(x=10,y=10)

        self.btn_01 = tk.Button(self.frame,text="Button1")
        self.btn_01.place(x=20,y=20)

        self.btn_02 = tk.Button(self.frame,text="Button2")
        self.btn_02.place(relx=0.5,rely=0.5,relwidth=0.5)

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

app = Application()
app.run()

Homework:

from tkinter import *
from tkinter import messagebox
import random

class Application(Frame):
    def __init__(self,master = None):
        super().__init__(master)
        self.master = master
        self.pack()
        self.createWidget()


    def createWidget(self):
        #distance between (padx,pady) and other components
        Entry(self).grid(row=0,column=0,columnspan=4,pady=10)

        btnText = (("MC","M+","M-","MR"),
                   ("C","±","/","x"),
                   (7,8,9,"-"),
                   (4,5,6,"+"),
                   (1,2,3,"="),
                   (0,".")
                   )
        for rindex,r in enumerate(btnText):
            for cindex,c in enumerate(r):
                if c == "=":
                    Button(self,text = c,width = 2)\
                    .grid(row = rindex+1,column=cindex,rowspan=2,\
                     sticky = NSEW)
                elif c == 0:
                    Button(self,text = c,width = 2)\
                    .grid(row = rindex+1,column=cindex,columnspan=2,\
                    sticky = NSEW)
                elif c == ".":
                    Button(self,text=c,width=2)\
                    .grid(row=rindex+1,column=cindex+1,\
                    sticky = NSEW)
                else:
                    Button(self,text=c,width=2)\
                    .grid(row = rindex+1,column=cindex,
                    sticky = NSEW)
        


root = Tk()
root.title("Calculator")
root.geometry("250x220+200+300")
app = Application(master = root)
root.mainloop()

dahan1999

Leave a Reply

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

Related Posts