python - Tkinter module learning

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/u014157109/article/details/85536592

The following procedure is used Frame, Checkbutton, Rediobutton, Button, Entry, Lable, the procedure of Example Text class:

from tkinter import *

class WidgetsDemo:
    def __init__(self):
        window = Tk()
        #set title
        window.title("Widgets Demo")

        #Frame:包含其它小构建的容器小构建,可以作为其它小构件的父容器
        frame1 = Frame(window)
        frame1.pack()
        #生成一个int对象
        self.v1 = IntVar()
        #添加 勾选按钮,可以勾选,self.v1.get() = 1;可以取消勾选,self.v1.get() = 0.
        #⚠️这里不能添加value参数,会报错
        cbtBold = Checkbutton(frame1,text = "Blod",variable = self.v1,\
        command = self.processCheckbutton)
        self.v2 = IntVar()
        #添加 单选按钮,注意command参数执行类方法
        rbRed = Radiobutton(frame1,text = "Red",bg = "red",variable = self.v2,
        value = 1,command = self.processRadiobutton)
        rbYellow =Radiobutton(frame1,text = "Yellow",bg = "yellow",variable = self.v2,
        value = 2,command = self.processRadiobutton) 
        #用网格管理器管理位置:一行三列
        cbtBold.grid(row = 1,column = 1)
        rbRed.grid(row = 1,column = 2)
        rbYellow.grid(row = 1,column = 3)
        #添加 普通按钮,调用destroy方法销毁窗口,或者quit方法结束循环。
        quitButton = Button(window,text = "Exit",bg = "red",command = window.quit)
        quitButton.pack(side = BOTTOM)

        frame2 = Frame(window)
        frame2.pack()
        #添加提示标签
        label = Label(frame2,text = "Enter your name: ")
        #生成string对象
        self.name = StringVar()
        #文本框,可以接受输入self.name
        entryName = Entry(frame2,textvariable = self.name)
        #普通按钮
        btButton = Button(frame2,text = "Get Name",fg = "red",
        command = self.processButton)
        #设置位置
        label.grid(row = 1,column = 1)
        entryName.grid(row = 1,column = 2)
        btButton.grid(row = 1,column = 3)

        #添加text文本框,显示,处理多行文本
        text = Text(window)#注意这里用window,不能用frame
        text.pack()
        #END索引号表示在最后插入
        text.insert(END,"Tip:\nThe best way to learn Tkinter is to read")
        text.insert(END," these carefully designed example to")
        #INSERT索引表示在光标处插入
        text.insert(INSERT," use them to creat your applications.\n")

        #在文本框中插入按钮
        textButton = Button(text,text = "push me",fg = "red",
        command = self.processShow)
        text.window_create(INSERT,window=textButton)
        





        window.mainloop()

    def processCheckbutton(self):
            print("check button is " + ("checked" if self.v1.get() ==1 else "unchecked"))
            print(self.v1.get())

    def processRadiobutton(self):
            #self.v2.get()得到的是value的值
            print(("red" if self.v2.get() ==1 else "yellow") + " is selected.")
            print(self.v2.get())

    def processButton(self):
        print("Your name is ",self.name.get())

    def processShow(self):
        print("Push your self,buddy")

        

WidgetsDemo()



Guess you like

Origin blog.csdn.net/u014157109/article/details/85536592