from tkinter import *
def Change(top,op_menu_var):
setting = op_menu_var.get()
print('修改分辨率为%s' %setting) #这里的setting会随选择的分辨率而变化
top.destroy()
def CreateTop():
top = Toplevel()
top.geometry('%dx%d' % (300,300))
top.title('Settings')
top.grab_set()
Label(top, text='分辨率:',font = ('微软雅黑', 15)).grid(row=0,padx=5,pady=5)
op_menu_var = StringVar()
op_menu_var.set(setting)
OptMenu = OptionMenu(top,op_menu_var,*geometry)
OptMenu.config(width=20)
OptMenu.grid(row=0,column=1,padx=5,pady=5)
# menubtton.config(bg = "GREEN")
Button(top,text= '确定',font = ('微软雅黑', 15), command = lambda :Change(top,op_menu_var)).grid(row=1,column=0)
Button(top,text= '取消',font = ('微软雅黑', 15), command = top.destroy).grid(row=1,column=1)
global setting
setting = '800x600'
geometry = ['800x600',
'1024x768',
'1360x800',
'1600x960',
'1920x1080']
root = Tk()
root.title('Demo')
root.geometry('%dx%d' % (root.winfo_screenwidth()/3,root.winfo_screenheight()/2))
textlabel = Label(root,
text = '测试GUI',
font = ('微软雅黑', 20),
fg = 'black',
)
textlabel.pack(padx=5,pady=5)
button5 = Button(root, text='设置分辨率', font = ('微软雅黑', 20), command = CreateTop)
button5.pack(padx=10, pady = 10)
button4 = Button(root, text = '打印分辨率',font = ('微软雅黑', 20), command =
lambda:print(setting)) #这里点击按钮,一直打印'800x600',即使修改分辨率也没用
button4.pack(padx=10, pady = 10)
mainloop()