StarGazerD
StarGazerD

Reputation: 37

AttributeError: 'str' object has no attribute 'tk' when running Tkinter

I was writing a python GUI program with Spleeter. And when it comes to the Separation function, the error occurs. Here's my code:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
from spleeter.separator import Separator
from tkinter import *
from tkinter.ttk import *
from tkinter import messagebox

window = Tk()
screen_width,screen_height = window.maxsize()
window.title("Spleeter GUI Version")
w = int((screen_width-700)/2)
h = int((screen_height-400)/2)
window.geometry(f'700x400+{w}+{h}')

lbl = Label(window, text="File Path:")
lbl.grid(column=0, row=0)

txt = Entry(window, width=10)
txt.grid(column=1, row=0)

lbl2 = Label(window, text="Stems:")
lbl2.grid(column=0, row=1)

combo = Combobox(window)
combo['values'] = (2,4,5)
combo.current(0)
combo.grid(column=1, row=1)

def Separation():
    File_name=txt.get();
    stems='spleeter:'+combo.get()+'stems'
    separator = Separator(stems)
    separator.separate_to_file(File_name, 'out')
    messagebox.showinfo("Notification", "Separation Finished!")


def clicked():
    Separation()

btn = Button(window, text="Separate", command=clicked)
btn.grid(column=2, row=0)

def main():
    window.mainloop()


if __name__=='__main__':
    main()

And the error occurs every time I click on the Button, the message is like:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\MyUsrname\.conda\envs\music\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "d:\File\Code\python\helloworld.py", line 39, in clicked
    Separation()
  File "d:\File\Code\python\helloworld.py", line 33, in Separation
    separator = Separator('spleeter:2stems')
  File "C:\Users\MyUsrname\.conda\envs\music\lib\tkinter\ttk.py", line 1138, in __init__
    Widget.__init__(self, master, "ttk::separator", kw)
  File "C:\Users\MyUsrname\.conda\envs\music\lib\tkinter\ttk.py", line 559, in __init__
    tkinter.Widget.__init__(self, master, widgetname, kw=kw)
  File "C:\Users\MyUsrname\.conda\envs\music\lib\tkinter\__init__.py", line 2292, in __init__
    BaseWidget._setup(self, master, cnf)
  File "C:\Users\MyUsrname\.conda\envs\music\lib\tkinter\__init__.py", line 2262, in _setup
    self.tk = master.tk
AttributeError: 'str' object has no attribute 'tk'

Upvotes: 1

Views: 475

Answers (1)

CristiFati
CristiFati

Reputation: 41116

separator = Separator(stems)

I assume you meant spleeter.separator.Separator (from [GitHub]: deezer/spleeter - Spleeter).

But (according to traceback), you ended up using [Python.Docs]: tkinter.ttk.Separator. And, that's due to (order is important too):

from spleeter.separator import Separator
# ...
from tkinter.ttk import *  # !!! THIS IS THE ONE !!!

Note that importing everything from a module is generally considered bad practice (with few exceptions, I personally consider that people using it kind of don't know what they are doing), so don't do it unless you know what it does behind the scenes.

Besides reusability and structure, packages and modules also act as (nested) namespaces, avoiding name clashes. So (most times), only import the package (or the module), and refer to classes, functions as (package.)module.function_or_class_or_variable_name

Example:

from spleeter import separator
sep = separator.Separator("a:b:c:d")

from tkinter import ttk
lbl = ttk.Label(window)

Upvotes: 1

Related Questions