【问题标题】:TKINTER, images mix when multiple window launchedTKINTER,多个窗口启动时图像混合
【发布时间】:2017-02-15 23:29:56
【问题描述】:

IMAGE: --->ScreenShot of the problem

IMAGE: --->How it is meant to look:

在上面的第一个超链接中,我附上了问题的截图。

运行通过按钮连接的窗口时,从第一个窗口启动的窗口上的图像显示在第一个窗口上,而不是在刚刚打开的新窗口上。

起初我以为python可能会因为文件名相同而感到困惑。 (情况并非如此,因为我使用了具有不同文件名的稍微不同的图像。

我尝试了很多方法,但都不起作用。

这一行的正下方是第一个窗口的代码:

#----Main Menu----
root_menu = Tk()
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,
                          text = "Search Inventory",
                          command=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",
                command = Add_To_Database,
                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)
root_menu.mainloop()

这是第二个窗口的代码。

def Search_Inventory():
#---Search Window----
root_search = Tk()
root_search.title("Warehouse Inventory Control System")
root_search.configure(bg = "black")

#----Title displayed under the company logo on the first window----
Title_Screen = Label(root_search,
                     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_search,
                    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_search,
                         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_search,
                       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_search,
             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_search,
                     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_search,
                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_search.mainloop()

【问题讨论】:

  • 如果它出现在错误的窗口中,那是因为它有错误的父级。仔细查看您的代码。
  • 如果父级的意思是在代码中说明实际窗口以显示图像,我试过了,它仍然没有改变任何东西

标签: python image python-3.x tkinter


【解决方案1】:

在你的问题中,你为你的“第二个窗口”展示了这个:

def Search_Inventory():
    #---Search Window----
    root_search = Tk()
    ...
    photoLabel = Label(image=photo)

上面的代码有两个问题。首先,您正在创建Tk 的第二个实例。你不应该这样做。一个 tkinter 程序应该只有一个 Tk 的实例。如果你想要更多的窗口,你需要创建Toplevel 的实例。

第二个问题是你在哪里创建Label——你忽略了给它一个父窗口,所以它可能默认为原始窗口。您需要将最后一行更改为:

photoLabel = Label(root_search, image=photo)

【讨论】:

  • 感谢您的回答,如何添加父母?
  • @MikiAmeryk:就像您向您创建的每个其他小部件添加父级一样。 Label(root_search, image=photo)
  • photo = PhotoImage(root_search, file="image.gif") photoLabel = Label(image=photo) photoLabel.image = photo photoLabel.grid(row=1, column=4, pady = 10 )
  • 我总是把它加到第一行
猜你喜欢
  • 1970-01-01
  • 2019-03-04
  • 2019-04-30
  • 2021-07-14
  • 1970-01-01
  • 1970-01-01
  • 2020-03-26
  • 2019-08-07
  • 1970-01-01
相关资源
最近更新 更多