![](/img/trans.png)
[英]Why tkinter module raises attribute error when run via command line but not when run via IDLE?
[英]Module working independently but raises an error when imported
所以我正在使用 Python 中的 tkinter.tix 模塊來制作可滾動的 window。 當我獨立運行模塊時它可以完美運行,但是當我導入它時,它會引發錯誤。 即使我在主程序中獨立導入 tkinter.tix 模塊,它也會導致主程序中的其他模塊出錯(來自 PIL 的圖像模塊在我導入 tkinter.tix 模塊之前完美運行,之后它當然會引發錯誤)。 有沒有人遇到過這個錯誤? 任何幫助將不勝感激。 謝謝
代碼:
主程序:
from PIL import Image, ImageTk
import TCRMenuOptions, STUMenuOptions
assignment = tk.Button( #This button calls the function to display assignments
master=frame,
text="Assignment",
font=font.Font(size=20, family="Helvetica"),
image = asimg,
compound = "top",
width=200,
height = 210,
activebackground="White",
bg="#33FFC5",
bd=2,
fg="DarkSlateGray",
command = lambda: STUMenuOptions.viewassign(cls)
)
STUMenuOptions 模塊:
def viewassign(cls):
root1 = Tk()
root1.config(background="#303939")
root1.state('zoomed')
frame = Frame(root1, bg="#303939", width="1366", height="768").grid(row=0, column=0)
swin = ScrolledWindow(frame, width="1366", height="768")
swin.grid(row=0, column=0)
root = swin.window
root.config(background="#303939")
cls_lbl = Label(root, text="Current Assignments", font=font.Font(size=50, weight="bold"),bg="#303939", fg="Cyan").grid(row = 0, column = 0)
# Plus some other non relevant code
So when the button is pressed in the main program, it calls the viewassign(cls) function from STUMenuOptions module which it does, but then a blank window appears (There should be atleast the label "Current Assignments"; atleast for the code I am在這里發布)它引發了我在之前的編輯中發布的錯誤,但我發布了完整的錯誤:
Traceback (most recent call last):
File "C:\Users\abc2\AppData\Local\Programs\Python\Python36\lib\tkinter\__init__.py", line 1699, in __call__
return self.func(*args)
File "C:/Users/abc2/Python/ComputerScienceProject/Main.py", line 324, in <lambda>
command = lambda: STUMenuOptions.viewassign(cls)
File "C:\Users\abc2\Python\ComputerScienceProject\STUMenuOptions.py", line 466, in viewassign
swin = ScrolledWindow(frame, width="1366", height="768")
File "C:\Users\abc2\AppData\Local\Programs\Python\Python36\lib\tkinter\tix.py", line 1348, in __init__
TixWidget.__init__(self, master, 'tixScrolledWindow', ['options'], cnf, kw)
File "C:\Users\abc2\AppData\Local\Programs\Python\Python36\lib\tkinter\tix.py", line 315, in __init__
self.tk.call(widgetName, self._w, *extra)
_tkinter.TclError: invalid command name "tixScrolledWindow"
你這樣做了:
frame = Frame(root1, bg="#303939", width="1366", height="768").grid(row=0, column=0)
在那里,您創建一個tk.Frame
並立即調用其.grid
方法並將.grid
的結果(始終為 None)存儲在變量frame
中。 然后,當您將其傳遞給ScrolledWindow(frame, ...)
時,它會混淆 tkinter。
你真正想做的是:
frame = Frame(root1, bg="#303939", width="1366", height="768")
frame.grid(row=0, column=0)
這個問題與這個問題非常相似。 始終拆分創建小部件並將小部件打包/放置/網格化在單獨的行上。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.