I am making tic tac toe in python using tkinter, and the code all works except for when I try to print which player is the winner, I get the following ouput:
The player who won is:
.!entry
The code I used to print out the winner is this:
print("The player who won is: ", e1_entry)
...
where e1_entry
is defined as this:
e1_entry = e1.get()
and e1 is:
e1 = tkinter.Entry(window, textvariable=e1_entry)
How would I make it so that the text taken from the tkinter Entry
is printed to the console?
Thanks
Gaurav Bhalla
Here is a working example. You would need to define a function to get the string
import tkinter
def getWinner():
n = entry1.get()
if n == "":
n = "Nobody won"
print(n)
root = tkinter.Tk()
entry1 = tkinter.Entry(root)
entry1.grid(row = 0, column = 0)
root.after(10000, getWinner)
root.mainloop()
This has an Entry widget, and after 10 seconds, will call the function 'getWinner'. If the Entry widget has no text in it, there will be no winner.
Hope this helps!
You should try something like:
window = Tk()
text = StringVar()
e1 = Entry(window, textvariable = text)
e1.pack()
e1_entry = text.get()
print("The player who won is:", e1_entry)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.