Tkinter 之OptionMenu下拉选择菜单

一、代码示例

import tkinter as tk

window = tk.Tk()
# 设置窗口大小
winWidth = 600
winHeight = 400
# 获取屏幕分辨率
screenWidth = window.winfo_screenwidth()
screenHeight = window.winfo_screenheight()

x = int((screenWidth - winWidth) / 2)
y = int((screenHeight - winHeight) / 2)

# 设置主窗口标题
window.title("Menu菜单参数说明")
# 设置窗口初始位置在屏幕居中
window.geometry("%sx%s+%s+%s" % (winWidth, winHeight, x, y))
# 设置窗口图标
window.iconbitmap("./image/icon.ico")
# 设置窗口宽高固定
window.resizable(0, 0)

var = tk.StringVar()
var.set("请选择")
tk.OptionMenu(window, var, "one", "two", "three").pack()
def getValue():
    print(var.get())
tk.Button(window, text="get value", width=30, pady=5, command=getValue).pack()
window.mainloop()

  

二、效果图

 

 

 

posted @ 2019-11-04 11:08  样子2018  阅读(4945)  评论(0编辑  收藏  举报