菜单、子窗口
* coding: UTF-8 *
#@time: 2022/2/8 11:33
#@Author: Bao
from tkinter import *
mainWin=Tk()
mainWin.title(‘Menu’)
width = 420
height = 300
#获取屏幕尺寸以计算布局参数,使窗口居屏幕中央
screenwidth = mainWin.winfo_screenwidth()
screenheight = mainWin.winfo_screenheight()
alignstr =’%dx%d+%d+%d’ % (width, height, (screenwidth-width)/2, (screenheight-height)/2)
mainWin.geometry(alignstr)
mainWin.resizable(width=False, height=True)
def setFuc():
entIp.delete(0,END)
entIp.insert(‘insert’,‘192.168.16.195’)
def openFuc():
entIp.delete(0,END)
entIp.insert(‘insert’,‘192.168.16.192’)
def newFuc():
entIp.delete(0,END)
entIp.insert(‘insert’,‘192.168.16.193’)
def conFuc():
def quitSub(): #关闭子窗口函数
t1.destroy()
def conSub(): #子窗口按钮函数
entIp.delete(0,END)
entIp.insert(‘insert’,‘192.168.16.196’)
quitSub()
entIp.delete(0,END)
entIp.insert(‘insert’,‘192.168.16.191’)
#创建子窗口
t1=Toplevel(mainWin)
t1.geometry(alignstr)
Label(t1, text=‘子窗口’).place(x=20,y=20)
Button(t1,text=‘确定’,command=conSub).place(x=20,y=50)
#创建顶级菜单
menubar=Menu(mainWin)
#创建下拉菜单
fileMenu=Menu(menubar)
fileMenu.add_cascade(label=‘open’,command=openFuc)
fileMenu.add_separator()
fileMenu.add_command(label=‘new’,command=newFuc)
#在顶级菜单添加二级菜单
menubar.add_cascade(label=‘File’,menu=fileMenu)
#一级菜单命令
menubar.add_command(label=‘Connect’,command=setFuc)
#显示菜单
mainWin.config(menu = menubar)
labIp=Label(mainWin,text=‘Ip’)
labIp.place(x=60,y=40,width=30,height=20)
entIp=Entry(mainWin)
entIp.insert(‘insert’,‘127.0.0.1’)
entIp.place(x=100,y=40,width=100,height=20)
varIp=StringVar()
varIp.set(‘确定’)
conBut=Button(mainWin,textvariable = varIp,command=conFuc)
conBut.place(x=160,y=70,width=30,height=20)
mainWin.mainloop()