Home > OS >  Python cant re-enter readline
Python cant re-enter readline

Time:07-17

I'm new to python and I'm trying to make a GUI window with Tkinter that executes a command. I wrote the code underneath but it wont work. What is wrong with it? The required imports are imported like tk and ttk.

This is the tkinter window code:

root = Tk()
root.geometry("600x450")
root.title("Points")
Label(root, text=PlayerPoints,font=('Ubuntu')).pack(pady=30)
btn= ttk.Button(root, text="Add more points",command=AddPoints)
btn.pack()

Here is the AddPoints commands code (not the full thing but enough to make the error show up):

PointsToAdd = int(input("How many points do you want to add?"))
print(PointsToAdd, "points will be added.")

And here is the error i get:

Exception has occurred: RuntimeError       (note: full exception trace is shown but execution is paused at: AddPoints)
can't re-enter readline

An example code to paste in a code editor to see the error yourself:

from tkinter import *
from tkinter import ttk
PlayerPoints = 100
def AddPoints():
    PointsToAdd = int(input("How many points do you want to add?"))
    print(PointsToAdd, "points will be added.")
root = Tk()
root.geometry("600x450")
root.title("Points")
Label(root, text=PlayerPoints,font=('Ubuntu')).pack(pady=30)
btn= ttk.Button(root, text="Add more points",command=AddPoints)
btn.pack()
n = input("this is just to make Python wait for an input instead of killing the program")

CodePudding user response:

From running the code you share, where the tk window will not run, the first clear error is that you haven't included a root.mainloop() statement

below is the code as I have tweaked it to make it run:

from tkinter import *
from tkinter import ttk

PlayerPoints = 100


def AddPoints():
    PointsToAdd = int(input("How many points do you want to add?"))
    print(PointsToAdd, "points will be added.")


root = Tk()
root.geometry("600x450")

Label(root, text=PlayerPoints, font=('Ubuntu')).pack(pady=30)
btn = ttk.Button(root, text="Add more points", command=AddPoints)
btn.pack()
root.mainloop()
n = input("this is just to make Python wait for an input instead of killing the program")

from what I have changed, there aren't any errors. Not too sure if this is what you meant or not though.

If you're unfamiliar with how the mainloop feature of tk works I'd suggest either doing some research on it or looking at the following article https://www.educba.com/tkinter-mainloop/

One final thing is that you don't have any code to update the points on the label in the tk GUI (not sure whether you wanted this to happen or not) the following screenshot is the output when running the code I attached above

console_output

  • Related