美文网首页
042 Python语法之图形界面开发tkinter

042 Python语法之图形界面开发tkinter

作者: Luo_Luo | 来源:发表于2017-09-12 11:35 被阅读0次

    tkinter

    简单一个窗体的创建

    import tkinter
    
    win = tkinter.Tk()  # 创建一个窗体
    win.title("我是标题")   # 标题
    win.geometry("800x600") # 窗体大小
    win.minsize(400, 400)   # 最小的范围
    win.maxsize(800, 800)   # 最大的范围
    win.mainloop() # 消息循环,让这个窗体显示
    

    win.mainloop()

    1. 如果每一个窗体都配置完成之后才调用这个方法,那么就是阻塞运行的
    2. 如果每一个窗体都是配置完成之后,然后统一调用这个方法,那么就是并发运行的

    控件标签label

    # win:控件放置在win窗体上
    # text:控件的名字
    label = tkinter.Label(win, text="wifi")
    
    label.pack()  # 防止在合适的位置
    
    # x:标签左上角的位置x坐标
    # y:标签左上角的位置y坐标
    label.place(x=10,y=10)#放在某个位置上
    

    按钮Button

    def close():
        import os
        os.system("shotdown -r -t 300")
    
    # win:控件放置在win窗体上
    # text:控件的名字
    # command:按下按钮
    button=tkinter.Button(win, text="wifi",command=close)
    
    # x:控件左上角的位置x坐标
    # y:控件左上角的位置y坐标
    button.place(x=10,y=10)#放在某个位置上
    

    PhotoImage

    # 创建一张图片,传递一个图片路径,图片必须是png格式的
    img = tkinter.PhotoImage(file=r"路径")
    # 设置图片
    label = tkinter.Label(win, image=img)
    

    八大对话框messagebox

    import tkinter
    import tkinter.messagebox
    
    
    def showinfo():
        msg = tkinter.messagebox.showinfo(title="我是 showinfo", message="我是 showinfo")
        print(msg)  # ok
    
    def showerror():
        msg = tkinter.messagebox.showerror(title="我是 showerror", message="我是 showerror")
        print(msg)  # ok
    
    def showwarning():
        msg = tkinter.messagebox.showwarning(title="我是 showwarning", message="我是 showwarning")
        print(msg)  # ok
    
    def askokcancel():
        msg = tkinter.messagebox.askokcancel(title="我是 askokcancel", message="我是 askokcancel")
        print(msg)  # True False
    
    def askquestion():
        msg = tkinter.messagebox.askquestion(title="我是 askquestion", message="我是 askquestion")
        print(msg)  # yes no
    
    def askretrycancel():
        msg = tkinter.messagebox.askretrycancel(title="我是 askretrycancel", message="我是 askretrycancel")
        print(msg)  #
    
    def askyesno():
        msg = tkinter.messagebox.askyesno(title="我是 askyesno", message="我是 askyesno")
        print(msg)  # True False
    
    def askyesnocancel():
        msg = tkinter.messagebox.askyesnocancel(title="我是 askyesnocancel", message="我是 askyesnocancel")
        print(msg)  # True False None
    
    
    win = tkinter.Tk()
    win.geometry("400x400")  # 设置窗体大小
    
    win.maxsize(500, 500)
    win.minsize = (300, 300)
    
    btn1 = tkinter.Button(win, text="showinfo", command=showinfo)
    btn2 = tkinter.Button(win, text="showerror", command=showerror)
    btn3 = tkinter.Button(win, text="showwarning", command=showwarning)
    btn4 = tkinter.Button(win, text="askokcancel", command=askokcancel)
    btn5 = tkinter.Button(win, text="askquestion", command=askquestion)
    btn6 = tkinter.Button(win, text="askretrycancel", command=askretrycancel)
    btn7 = tkinter.Button(win, text="askyesno", command=askyesno)
    btn8 = tkinter.Button(win, text="askyesnocancel", command=askyesnocancel)
    btn1.place(x=0, y=0)
    btn2.place(x=0, y=40)
    btn3.place(x=0, y=80)
    btn4.place(x=0, y=120)
    btn5.place(x=0, y=160)
    btn6.place(x=0, y=200)
    btn7.place(x=0, y=240)
    btn8.place(x=0, y=280)
    
    win.mainloop()
    

    Entry:文字输入

    entry = tkinter.Entry(win)  # 输入
    
    

    Listbox:

    mylist = tkinter.Listbox(win, width=100)  # 输入
    
    

    相关文章

      网友评论

          本文标题:042 Python语法之图形界面开发tkinter

          本文链接:https://www.haomeiwen.com/subject/hphcsxtx.html