簡體   English   中英

為什么我添加背景時 TKinter 打開 2 windows

[英]Why does TKinter open 2 windows when I add a background

I'm trying to add a background to my Tkinter window, but when I use this code, then It opens the background as a different Tkinter window and the main window as a seperate one. 我怎樣才能把它們合二為一?

from tkinter import *
from tkinter.filedialog import askopenfilename
from tkinter import messagebox

top = Tk()

C = Canvas(top, bg ="blue", height=250, width=300)
filename = PhotoImage(file = "C:/Users/plapl/Desktop/ching.pgm")
background_label = Label(top, image=filename)
background_label.place(x=0, y=0, relwidth=1, relheight=1)

C.pack()
top.mainloop()


def newfile():
    print("New File!")


root = Tk()
menu = Menu(root)
root.config(menu=menu)
filemenu = Menu(menu)
menu.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="New", command=newfile)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=root.quit)


mainloop()

我已經簡化了你的代碼,現在它適用於我 1 window 並且我導入了一個你可能錯過的模塊(PIL)

代碼:

from tkinter import *
from PIL import ImageTk
from tkinter.filedialog import askopenfilename
from tkinter import messagebox

top = Tk()

C = Canvas(top, bg ="blue", height=250, width=300)
filename = ImageTk.PhotoImage(file = "C:/Users/plapl/Desktop/ching.pgm")
background_label = Label(top, image=filename)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
C.pack()


def newfile():
    print("New File!")


menu = Menu(top)
top.config(menu=menu)
filemenu = Menu(menu)
menu.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="New", command=newfile)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=top.quit)


top.mainloop()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM