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()

  

二、效果圖

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM