pythontkinterimportmoduletix

Module working independently but raises an error when imported


So I was working with tkinter.tix module in Python to make a Scrollable 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). 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:


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:

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"



Solution

  • 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. Then when you pass it in to ScrolledWindow(frame, ...) it confuses 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.