【问题标题】:root = Tk(), are there any alternatives?root = Tk(),还有其他选择吗?
【发布时间】:2017-02-05 20:54:10
【问题描述】:

有没有人知道任何替代方案: root = tk()

当我运行我的程序时,多个窗口同时运行,只是想知道是否有替代方法使它们不会同时打开。程序运行后立即打开的第二个窗口应在按下初始窗口上的按钮时启动。我也尝试过使用: 根 = 顶层() 但结果是一样的。 有任何想法吗?谢谢

def Search_Inventory():
#---First Window----
root = Toplevel()
root.title("Warehouse Inventory Control System")
root.configure(bg = "black")

#----Title displayed under the company logo on the first window----
Title_Screen = Label(root,
                     text="Warehouse Inventory Control System",
                     fg="grey",
                     bg="black",
                     font="Helevetica 25 bold",
                     pady = "50",
                     padx = "50",
                     ).grid(row=3, column=4)

#----Interactive Input Boxes for the User----

#----Label to Notify what is needed in the entry box----

PN_Info_Label = Label(root,
                    text = "Part Number:",
                    fg="white",
                    bg="black",
                    font="Helevetica 15 bold",
                    padx = 50,
                    ).grid(row=14, column=3, rowspan = 2)

#----Input Box for Part Number

PN_Display = StringVar()
Input_PartNumber = Entry(root,
                         textvariable=PN_Display,
                         fg = "blue",
                         font = "Helevtica 25 bold",
                         ).grid(row=14, column=4)

#----A button that will proceed to the next part of the program----

Search_Button = Button(root,
                       text = "Search Inventory",
                       fg = "Blue",
                       bg = "Grey",
                       bd = 2,
                       font="Helevetica 15 bold",
                       command=lambda:Search_Prod(PN_Display.get()),
                       height = 1,
                       width = 15,
                       ).grid(row=16, column=4,pady = 25,padx = 25,)

#----Information regarding how to enter the part number---
Info = Label(root,
             text="Please Use Capitals to Enter Part Number",
             fg= "red",
             bg = "black",
             font = "helevetica 12 bold",
             ).grid(row = 15, column = 4)
#----Adding the company logo to the first window----

photo = PhotoImage(file="image.gif")
photoLabel = Label(image=photo)
photoLabel.image = photo
photoLabel.grid(row=1, column=4, pady = 10)

#----Linking the Help Document----

Help_Button = Button(root,
                     text = "Help",
                     command=Help_PDF,
                     fg = "Red",
                     bg = "Black",
                     height = "1",
                     bd = "2",
                     font = "Helevetica 20 bold",
                     width = "4",
                     ).grid(row=0, column=5, pady= 10,padx = 50, sticky = E)

#----Saying who the program was made by----

Credits = Label(root,
                text = "Created By: Me",
                fg = "White",
                bg = "Black",
                font = "Helevetica 10 underline",
                ).grid(row = 19, column = 5)
#To Make Sure that the window doesn't close
root.mainloop()

这就是本来打算稍后运行但立即运行的子程序。

root_menu = frame()
root_menu.title("Warehouse Inventory Control System")
root_menu.configure(bg = "black")


photo = PhotoImage(file="logo.gif")
photoLabel = Label(image=photo)
photoLabel.image = photo
photoLabel.grid(row=1, column=3,columnspan = 4, sticky = N)

Title_Screen = Label(root_menu,
                 text="Welcome to the SsangYong\n Warehouse Inventory                                      Control System",
                 fg="grey",
                 bg="black",
                 font="Helevetica 25 bold",
                 pady = "50",
                 padx = "50",
                 ).grid(row=2, column=3)


Search_Inventory = Button(root_menu,
                      command = Search_Inventory(),
                      text = "Search Inventory",
                      fg = "Blue",
                      bg = "Grey",
                      bd = 2,
                      font="Helevetica 12 bold",
                      height = 1,
                      width = 50,
                      ).grid(row=16, column=3,pady = 25,padx = 25,)

Add_Stock = Button(root_menu,
            text = "Add New Items",
            fg = "Blue",
            bg = "Grey",
            bd = 2,
            font="Helevetica 12 bold",
            height = 1,
            width = 60,
            ).grid(row=15, column=3,pady = 25,padx = 25,)

Print_Report =  Button(root_menu,
            text = "Print Stock Report",
            fg = "Blue",
            bg = "Grey",
            bd = 2,
            font="Helevetica 12 bold",
            height = 1,
            width = 70,
            ).grid(row=17, column=3,pady = 25,padx = 25,)


Help_Button = Button(root_menu,
                 text = "Help",
                 command=Help_PDF,
                 fg = "Red",
                 bg = "Black",
                 height = "1",
                 bd = "2",
                 font = "Helevetica 20 bold",
                 width = "4",
                 ).grid(row=1, column=3,rowspan = 2, sticky= NE)

Credits = Label(root,
                text = "Created By:mk",
                fg = "White",
                bg = "Black",
                font = "Helevetica 10 underline",
                ).grid(row = 19, column = 5)

root_menu.mainloop() 

这是最初要弹出的内容,但与另一个完全混淆。

【问题讨论】:

  • 为什么在第二个文件的最后一个Label 中使用root 而不是root_menu
  • 顺便说一句:var = Widget().grid() 它将None 分配给变量,因为grid() 返回None - 如果您需要var,那么您必须分两步进行var = Widget()var.grid()

标签: python python-3.x tkinter project root


【解决方案1】:

有人知道root = tk() [原文如此]

别无选择。 tkinter 应用程序绝对必须有一个根窗口。如果您没有显式创建一个(并且您应该),则会在您第一次创建任何其他小部件时为您创建一个。

程序运行后立即打开的第二个窗口应在按下初始窗口上的按钮时启动。

这样做的原因是您告诉它立即打开。看看这段代码:

Search_Inventory = Button(root_menu,
                  command = Search_Inventory(),

以上代码与此相同:

result = Search_Inventory()
Search_Inventory = Button(root_menu, command=result)

请注意命令中缺少()command 属性需要一个函数的引用

【讨论】:

    【解决方案2】:

    command= 需要函数名 - 回调 - 这意味着没有 ()

    但是你有()

      command = Search_Inventory()
    

    这就是为什么它在开始时执行,而不是在您单击按钮时执行


    顺便说一句:tkinter 只需要一个 mainloop() - 如果您使用两个 mainloop(),那么它就无法正常工作。


    编辑:图片问题是因为您没有在第二个窗口中为Label 设置父级

    photoLabel = Label(image=photo)
    

    所以它默认使用主窗口

    你需要

    photoLabel = Label(root, image=photo)
    

    编辑:最少的工作代码

    from tkinter import *
    
    def Help_PDF():
        pass
    
    def Search_Inventory():
    
        #--- Second Window ----
        root = Toplevel()
    
        Title_Screen = Label(root, text="Warehouse Inventory Control System")
        Title_Screen.grid(row=3, column=4)
    
        PN_Info_Label = Label(root, text="Part Number:")
        PN_Info_Label.grid(row=14, column=3, rowspan=2)
    
        PN_Display = StringVar()
    
        Input_PartNumber = Entry(root, textvariable=PN_Display)
        Input_PartNumber.grid(row=14, column=4)
    
        Search_Button = Button(root, text="Search Inventory",
                               command=lambda:Search_Prod(PN_Display.get()))
        Search_Button.grid(row=16, column=4, pady=25, padx=25)
    
        Info = Label(root, text="Please Use Capitals to Enter Part Number")
        Info.grid(row=15, column=4)
    
        photo = PhotoImage(file="image.gif")
    
        photoLabel = Label(root, image=photo)
        photoLabel.image = photo
        photoLabel.grid(row=1, column=4, pady=10)
    
        Help_Button = Button(root, text="Help",
                             command=Help_PDF)
        Help_Button.grid(row=0, column=5, pady=10, padx=50, sticky=E)
    
        Credits = Label(root, text="Created By: Me")
        Credits.grid(row=19, column=5)
    
    
    # --- First Window ---
    
    root_menu = Tk()
    
    photo = PhotoImage(file="logo.gif")
    photoLabel = Label(root_menu, image=photo)
    photoLabel.image = photo
    photoLabel.grid(row=1, column=3, columnspan=4, sticky=N)
    
    Title_Screen = Label(root_menu, text="Welcome to the SsangYong\n Warehouse Inventory                                      Control System",)
    Title_Screen.grid(row=2, column=3)
    
    Search_Inventory = Button(root_menu, text="Search Inventory",
                          command=Search_Inventory)
    Search_Inventory.grid(row=16, column=3, pady=25, padx=25)
    
    Add_Stock = Button(root_menu, text="Add New Items")
    Add_Stock.grid(row=15, column=3, pady=25, padx=25)
    
    Print_Report =  Button(root_menu, text="Print Stock Report")
    Print_Report.grid(row=17, column=3, pady=25, padx=25)
    
    Help_Button = Button(root_menu, text="Help",
                     command=Help_PDF)
    Help_Button.grid(row=1, column=3, rowspan=2, sticky=NE)
    
    Credits = Label(root_menu, text="Created By:mk")
    Credits.grid(row=19, column=5)
    
    root_menu.mainloop()
    

    【讨论】:

    • 感谢这确实有效,现在唯一的问题是图像混合了
    • 描述问题
    • 问题可能是变量的一些错误或第二个mainloop() 造成问题。
    • @MikiAmeryk 我认为你必须使用root_menu = Tk() 而不是root_menu = frame()
    • @MikiAmeryk 查看图片解决方案。
    猜你喜欢
    • 1970-01-01
    • 2015-08-22
    • 2011-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-26
    相关资源
    最近更新 更多