thinker组件 OptionMenu

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)