我正在制作一個 GUI,這與來自另一個專案的完全相同的代碼是小部件在self其父級時出現。但是出于某種原因,在這個腳本中,它們只會顯示是否root設定為父級,為什么?如果我切換父母,影像標簽不會顯示,但框架會顯示,反之亦然。
import tkinter as tk
import PIL.Image
import PIL.ImageTk
import sys, os
#GUI Class
class MainGUI(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self,parent)
self.parent=parent
self.test()
def test(self):
self.bImage = PIL.ImageTk.PhotoImage(PIL.Image.open("Background.png"))
self.bLabel = tk.Label(self, image=self.bImage)
self.bLabel.image=self.bImage
self.bLabel.place(x=0,y=0,width=300,height=400)
self.test = tk.Frame(root, bg="red")
self.test.place(x=10,y=10,width=50,height=50)
if __name__ == "__main__":
root=tk.Tk()
root.title("Buddy Swap")
root.geometry("300x400")
root.resizable(0,0)
root.iconbitmap(os.path.dirname(sys.argv[0]) "\\Icon.ico")
MainGUI(root).pack()
root.mainloop()
uj5u.com熱心網友回復:
問題是您正在使用place標簽。place不會導致包含的小部件增長或縮小,因此框架保持其默認大小 1x1 像素。
除非你非常小心,否則place這些問題很容易發生,這也是我在幾乎所有情況下都推薦的原因之一grid。packplace
一個簡單的解決方法是給你的MainGUI框架一個大小,以便里面的小部件可見,或者停止使用place.
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/490838.html
標籤:Python 图片 用户界面 tkinter python成像库
上一篇:來自代碼背后的WPF影片不起作用
下一篇:在C中運行WinMain的問題
