May-02-2022, 02:52 AM
Hey guys, I'm trying to make a magic 8 ball interface using Tkinter. I'm fairly new to coding, and I guess you could say this is my first "personal" project without having to use tutorials or read any books. I'm trying to have this code print a random response when you press the button, but I get these callbacks.
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Harrison\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 1921, in __call__
return self.func(*args)
File "C:\Users\Harrison\PycharmProjects\8ball\8ball.py", line 37, in <lambda>
shake_button = tk.Button(root, textvariable=shake_text, command=lambda:shake())
File "C:\Users\Harrison\PycharmProjects\8ball\8ball.py", line 30, in shake
text_box.insert(1.0, responses.EightBall())
File "C:\Users\Harrison\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 3772, in insert
self.tk.call((self._w, 'insert', index, chars) + args)
_tkinter.TclError: wrong # args: should be ".!text insert index chars ?tagList chars tagList ...?"
Here is my code.
Thanks for your time and help.
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Harrison\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 1921, in __call__
return self.func(*args)
File "C:\Users\Harrison\PycharmProjects\8ball\8ball.py", line 37, in <lambda>
shake_button = tk.Button(root, textvariable=shake_text, command=lambda:shake())
File "C:\Users\Harrison\PycharmProjects\8ball\8ball.py", line 30, in shake
text_box.insert(1.0, responses.EightBall())
File "C:\Users\Harrison\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 3772, in insert
self.tk.call((self._w, 'insert', index, chars) + args)
_tkinter.TclError: wrong # args: should be ".!text insert index chars ?tagList chars tagList ...?"
Here is my code.
import tkinter as tk from PIL import Image, ImageTk from tkinter.filedialog import askopenfile import responses root = tk.Tk(className="Magic 8 Ball") canvas = tk.Canvas(root, width=800, height=500) canvas.grid(columnspan=3, rowspan=3) #logo logo = Image.open("image.png") logo = ImageTk.PhotoImage(logo) logo_label = tk.Label(image=logo) logo_label.image = logo logo_label.grid(column=1, row=0) #instructions instructions = tk.Label(root, text="Please type in your question. Then 'Shake' to see your fate.") instructions.grid(columnspan=3, column=0, rowspan=5, row=1) def shake(): text_box = tk.Text(root, height=10, width=50, padx=15, pady=15) text_box.insert(1.0, responses.EightBall()) <-------------------- THIS IS WHERE MY ISSUE IS text_box.tag_configure("center", justify="center") text_box.tag_add("center", 1.0, "end") text_box.grid(column=1, row=3) #shake button shake_text = tk.StringVar() shake_button = tk.Button(root, textvariable=shake_text, command=lambda:shake()) shake_text.set("Shake") shake_button.grid(column=1, row=2) root.mainloop()the responses.py isn't necessary, it's just a simple random generator. but I run into the trouble at the spot that I highlighted (LINE 30). I can have the program print a statement, but I can't have it run my function of randomly printing a response.
Thanks for your time and help.