Python 7 Levels, L7, Unit 80, More Component in tkinter

Powered By EmbedPress

Exercise 1:

import tkinter as tk

class Application:

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


    def createWidget(self):
        self.v = tk.StringVar()
        self.v.set("Children's programming")
        self.om = tk.OptionMenu(self.root, \
                                self.v, "Children's programming", "Maker Education", "robot")
        self.om["width"] = 15
        self.om.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("Moving Slider Test")
        self.root.geometry("300x200+100+100")
        self.createWidget()

    def createWidget(self):
        self.scale = tk.Scale(self.root,from_ = 10,to=50,\
                              length=200,orien="horizontal",\
                              command=self.test1)
        self.scale.pack()
        self.label = tk.Label(self.root,text="Mathton Coding",\
                              fg="white",bg="black")
        self.label.pack()

    def test1(self,value):
        newFont = ("Times New Roman",value)
        self.label["font"] = newFont

    def run(self):
        self.root.mainloop()
app = Application()
app.run()

Exercise 3:

import tkinter as tk
from tkinter import colorchooser

class Application:

    def __init__(self):
        self.root = tk.Tk()
        self.root.title("Color Pick Dialog")
        self.root.geometry("300x200+100+100")
        self.creatWidget()

    def creatWidget(self):
        self.btn = tk.Button(self.root,text="Choose background color",command=self.test)
        self.btn.pack()

    def test(self):
        c = tk.colorchooser.askcolor(color="red",title = "Color Pick Dialog")
        print(c[1])
        self.root.config(bg=c[1])

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

app = Application()
app.run()

Exercise 4:

import tkinter as tk
from tkinter import filedialog

class Application:

    def __init__(self):
        self.root = tk.Tk()
        self.root.title("File Dialog Test")
        self.root.geometry("400x100+100+100")
        self.createWidget()

    def createWidget(self):
        self.btn = tk.Button(self.root,text="Please pick the video to be edited",\
                             command=self.test)
        self.btn.pack()
        self.label = tk.Label(self.root,width=30,height=2,bg="green")
        self.label.pack(pady=10)

    def test(self):
        name = tk.filedialog.askopenfilename(title="Choose video file",\
                                             initialdir="d:/file",\
                                             filetypes=[("Video Files",".mp4")])
        self.label["text"] = name
    def run(self):
        self.root.mainloop()

app = Application()
app.run()

Exercise 5:

import tkinter as tk
from tkinter import filedialog

class Application:

    def __init__(self):
        self.root = tk.Tk()
        self.root.title("File Dialog Test")
        self.root.geometry("400x100+100+100")
        self.createWidget()

    def createWidget(self):
        self.btn = tk.Button(self.root,text="Choose a text file",command=self.test)
        self.btn.pack()

        self.label = tk.Label(self.root,width=30,height=2,bg="green")
        self.label.pack(pady=10)

    def test(self):
        with tk.filedialog.askopenfile(title="Choose Text File",initialdir="d:\file",\
                                   filetypes=[("Text Files",".txt")]) as f:
            self.label["text"]=f.read()
        
    def run(self):
        self.root.mainloop()

app = Application()
app.run()

Exercise 6:

import tkinter as tk
from tkinter import filedialog

class Application:

    def __init__(self):
        self.root = tk.Tk()
        self.root.title("Text Editor")
        self.root.geometry("400x220+100+100")
        self.filename = None
        self.createWidget()

    def createWidget(self):
        self.btn01 = tk.Button(self.root,text="Open Text File",command = self.test1)
        self.btn01.pack()
        self.text = tk.Text(self.root,width=40,height=10)
        self.text.pack()
        self.btn02 = tk.Button(self.root,text="Save Text File",command=self.test2)
        self.btn02.pack()

    def test1(self):
        with tk.filedialog.askopenfile(title="Open Text File",initialdir="d:\file",\
                                  filetypes=[("Text File",".txt")]) as file:
            self.text.insert(1.0,file.read())
            self.filename = file.name

    def test2(self):
        with open(self.filename,"w") as file:
            r = self.text.get(1.0,"end")
            file.write(r)

    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