美文网首页
thinker组件 OptionMenu

thinker组件 OptionMenu

作者: HC2 | 来源:发表于2022-01-22 14:58 被阅读0次

    OptionMenu(选择菜单)

    • 新建一个下拉选择框
    import tkinter as tk
    
    window = tk.Tk()
    window.title('hello thinter')
    height= window.winfo_screenheight()
    width= window.winfo_screenwidth()
    window.geometry('400x300+%d+%d'%((width-400)/2,(height-300)/2))
    
    variable = tk.StringVar()
    variable.set("one")
    
    w = tk.OptionMenu(window, variable, "one", "two", "three")
    w.pack()
    
    window.mainloop()
    
    • 获取选中的值
    import tkinter as tk
    
    def A():
        print("点击调用",variable.get())
    
    window = tk.Tk()
    window.title('hello thinter')
    height= window.winfo_screenheight()
    width= window.winfo_screenwidth()
    window.geometry('400x300+%d+%d'%((width-400)/2,(height-300)/2))
    
    variable = tk.StringVar()
    variable.set("one")
    list1 = ["青菜", "白菜", "菠菜", "黄瓜"]
    w = tk.OptionMenu(window, variable, *list1)
    w.pack()
    
    tk.Button(window,text="获取文本值",command=A).pack()
    
    window.mainloop()
    

    或者:

    import tkinter as tk
    
    def A():
        print("点击调用",variable.get())
    
    window = tk.Tk()
    window.title('hello thinter')
    height= window.winfo_screenheight()
    width= window.winfo_screenwidth()
    window.geometry('400x300+%d+%d'%((width-400)/2,(height-300)/2))
    
    variable = tk.StringVar()
    variable.set("one")
    
    w = tk.OptionMenu(window, variable, "one", "two", "three")
    w.pack()
    
    tk.Button(window,text="获取文本值",command=A).pack()
    
    window.mainloop()
    
    image.png

    相关文章

      网友评论

          本文标题:thinker组件 OptionMenu

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