r/Tkinter • • Apr 17 '19

AttributeError: 'str' object has no attribute 'tk'

Title (line 21). I have been trying to create a text box, but Python is yelling at me. Code:

import tkinter

from tkinter import *

class Interface(Frame):

def __init__(self, master=None):

super(Interface, self).__init__()

Frame.__init__(self, master)

self.init_window()

def quit():

window.destroy()

def init_window(self):

self.master.title('Communications Interface')

self.pack(fill=BOTH, expand=1)

self.configure(background = 'black')

quitButton = Button(self, text='Quit', bg = 'black', fg = 'green', activebackground = 'black', activeforeground = 'red', command = quit)

quitButton.place(x=0, y=0)

def recvAndDisplay(self):

global allMsgs

msgs = Text(self, height = 400, width = 600, bg = 'black', fg = 'green', )

msgs.pack(side=LEFT, fill=Y)

scrollBar = Scrollbar(self, bg = 'black', fg = 'green', activebackground = 'green', activeforeground = 'black')

scrollBar.pack(side=RIGHT, fill=Y)

scrollBar.config(command=msgs.yview)

msgs.config(yscrollcommand=scrollBar.set)

while True:

msgs.insert('\\n', END, msg)

allMsgs = []

window = Tk()

window.geometry('800x500')

app = Interface(window)

window.mainloop()

msg = 'Test'

Interface.recvAndDisplay(msg)

msg = 'Test2'

Interface.recvAndDisplay(msg)

1 Upvotes

1 comment sorted by

2

u/woooee Apr 17 '19

There is no way to tell what str object the error message is referring to from the little that you posted. Post the complete error message trackback. Also, don't import tkinter twice, pick one or the other

import tkinter
from tkinter import *