如何在tkinter中清除整个屏幕并在单击菜单栏项时获取新数据

2024-04-25 05:59:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使一个基本的软件,其中,我有一个菜单栏和每一项点击,我想要一个完全不同的窗口布局

我不想打开一个新的屏幕,因为你们中的一些人可能会混淆,相反,我想让屏幕清晰起来,新的数据根据菜单栏选项点击打开在同一个窗口

如果你能帮我度过难关,那会很有帮助的。 我在下面附上我的代码:)

from tkinter import *
def doNothing():
    print("Ok, I wont")

#---------------------------Initializing----------------------------------------
root =Tk()

def clear():
    list = root.grid_slaves()
    print(list)
    for l in list:
        l.destroy()

#-----setting screen size by dynamically getting height and width of screen-----
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
root.geometry(str(screen_width)+"x"+str(screen_height))
root.config(background='#2b2b2b')


def home():
    labelfont = ('helvetica', 100)

    label_one = Label(root, text = 'Home page')
    label_one.config(fg='white',bg="#2b2b2b")  
    label_one.config(font=labelfont)        
    label_one.pack()





#------------------------adding menu bar to the window-------------------------
def menucall():
    menu = Menu(root)
    root.config(menu=menu)
    subMenu = Menu(menu)
    menu.add_cascade(label="File", menu=subMenu)
    subMenu.add_command(label="New Loan",command=newLoan)
    subMenu.add_command(label="New Invoice",command=doNothing)
    subMenu.add_separator()
    subMenu.add_command(label="Exit", command=home)


#-----------------------------------File---------------------------------------
#---------------------------------New Loan-------------------------------------

def newLoan():
    labelfont = ('helvetica', 100)
    label_one = Label(root, text = 'FinanCo New Loan').grid(row=0)
    label_one.config(fg='white',bg="#2b2b2b")  
    label_one.config(font=labelfont)        
    label_one.pack()

#---------------------------Main--------------------------------
menucall()



root.mainloop()

屏幕不清楚,相反,新的项目只是不断被添加到彼此下面


Tags: addconfignew屏幕defrootscreenone
1条回答
网友
1楼 · 发布于 2024-04-25 05:59:17

这可能是一种懒惰的方法,但要尽量隐藏像远离屏幕的项目(x=-100000,y=-100000)

单击菜单1时

#change all the items that you don't need in menu1
menu2label1.config(x=-100000,y=-100000)
menu2label2.config(x=-100000,y=-100000)
etc..

单击菜单2时

#change all the items that you don't need in menu2
menu1label1.config(x=-100000,y=-100000)
menu1label2.config(x=-100000,y=-100000)
etc..

更专业的做法是,尝试将项目粘贴在画布上,并在单击每个菜单时隐藏它们

老实说,我没有尝试过专业的方法,但值得一试

相关问题 更多 >