简体   繁体   English

模块独立工作,但导入时出现错误

[英]Module working independently but raises an error when imported

So I was working with tkinter.tix module in Python to make a Scrollable window.所以我正在使用 Python 中的 tkinter.tix 模块来制作可滚动的 window。 It works flawlessly when I run the module independently but when I import it, it raises an error.当我独立运行模块时它可以完美运行,但是当我导入它时,它会引发错误。 Even if I independently import the tkinter.tix module in the main program, it causes error with other module in the main program( Image module from PIL which works perfectly until I import the tkinter.tix module, after which it of course raises an error).即使我在主程序中独立导入 tkinter.tix 模块,它也会导致主程序中的其他模块出错(来自 PIL 的图像模块在我导入 tkinter.tix 模块之前完美运行,之后它当然会引发错误)。 Has anyone encountered this error before?有没有人遇到过这个错误? Any help will be really appreciated.任何帮助将不胜感激。 Thanks谢谢

Code:代码:

Main Program:主程序:

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 Module: 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 posting here)and it raises the error which I have posted in previous edit but I am posting the full one: 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"


You did this:你这样做了:

frame = Frame(root1, bg="#303939", width="1366", height="768").grid(row=0, column=0)

In there you create a tk.Frame and immediately call its .grid method and store the result from the .grid (which is always None) in the variable frame .在那里,您创建一个tk.Frame并立即调用其.grid方法并将.grid的结果(始终为 None)存储在变量frame中。 Then when you pass it in to ScrolledWindow(frame, ...) it confuses tkinter.然后,当您将其传递给ScrolledWindow(frame, ...)时,它会混淆 tkinter。

What you actually wanted to do is this:你真正想做的是:

frame = Frame(root1, bg="#303939", width="1366", height="768")
frame.grid(row=0, column=0)

This problem is very similar to this one.这个问题与这个问题非常相似。 Always split creating the widget and packing/placing/gridding the widget on separate lines.始终拆分创建小部件并将小部件打包/放置/网格化在单独的行上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM