[Tkinter] .get() doesn't exist? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: [Tkinter] .get() doesn't exist? (/thread-21764.html) |
.get() doesn't exist? - GalaxyCoyote - Oct-13-2019 I am trying to make a game that displays a word with a colour and the user has to type the colour into the box instead of the word. But there is an issue, .get() doesn't work. I get an error saying that Tkinter has no attribute .get(), I am extremely new to Tkinter (actually this is my first program on it). Here is the code: import tkinter from tkinter import * import random Pinput = 0 #var Loop = True #this is for the game loop Tcolour = str #Text colour Ttext = str #Words points = 0 #counts points def submit(): Pinput = Box.get() print(Pinput) if Pinput == Tcolour: points = points + 1 window = Tk() #screen setup window.title("Colour game recreation") window.geometry("500x300") window.configure(background = "black") window.resizable(0,0) #makes the window unscaleable Box = Entry(window) .grid(row = 2, column = 3) #text entry setup Button(window, text = "SUBMIT", command = submit) .grid(row = 3, column = 3) #Submit button Label(window, text = points, bg = "black", fg = "white", font = "none 15 bold") .grid(row = 5, column = 3) #score while Loop: Ttext = random.randint(1,9) #setting up text if Ttext == 1: Ttext = "Red" elif Ttext == 2: Ttext = "Blue" elif Ttext == 3: Ttext = "Green" elif Ttext == 4: Ttext = "Orange" elif Ttext == 5: Ttext = "Yellow" elif Ttext == 6: Ttext = "Purple" elif Ttext == 7: Ttext = "Brown" elif Ttext == 8: Ttext = "Black" elif Ttext == 9: Ttext = "White" Tcolour = random.randint(1,9) #setting up colours if Tcolour == 1: Tcolour = "Red" elif Tcolour == 2: Tcolour = "Blue" elif Tcolour == 3: Tcolour = "Green" elif Tcolour == 4: Tcolour = "Orange" elif Tcolour == 5: Tcolour = "Yellow" elif Tcolour == 6: Tcolour = "Purple" elif Tcolour == 7: Tcolour = "Brown" elif Tcolour == 8: Tcolour = "Black" elif Tcolour == 9: Tcolour = "White" Label(window, text = Ttext, bg = "black", fg = Tcolour, font = "none 30 bold") .grid(row = 1, column = 3) #word display window.mainloop()Python Version: 3.6.1 Thanks in advance! RE: .get() doesn't exist? - Axel_Erfurt - Oct-13-2019 try Box=Entry(window,width=15) Box.grid(row=2, column=3,sticky=E) RE: .get() doesn't exist? - GalaxyCoyote - Oct-13-2019 (Oct-13-2019, 03:52 PM)Axel_Erfurt Wrote: try The Entry shows up, the issue I am having is the fact that I cant get the data from that entry RE: .get() doesn't exist? - Axel_Erfurt - Oct-13-2019 What I write in the box I get as output, what should happen with it? RE: .get() doesn't exist? - GalaxyCoyote - Oct-13-2019 (Oct-13-2019, 05:14 PM)Axel_Erfurt Wrote: What I write in the box I get as output, what should happen with it? It gets stored in a var (Pinput) and then an if statement checks if the user has entered the correct colour RE: .get() doesn't exist? - joe_momma - Oct-13-2019 my observation of your code is you don't need the while loop- tkinter is using the mainloop to cycle events everything in your loop could be put in a function and updated with correct guess. an easy way of getting random colors is: >>> from random import choice >>> list_colors= ['red','green','blue'] >>> choice(list_colors) 'red' >>> choice(list_colors) 'green' >>>in your function add global points points = 0 #counts points def submit(): global points Pinput = Box.get() ,,,,,,,,have seen guizero? it's a module based on tkinter great for starting and basic interfaces guizero found here the reason i'm suggesting it is because all your labels could be textvariables and in guizero would make them by default. RE: .get() doesn't exist? - GalaxyCoyote - Oct-14-2019 (Oct-13-2019, 10:11 PM)joe_momma Wrote: my observation of your code is you don't need the while loop- Thanks! I will also look into guizero, GUI (Tkinter) is just a side project to keep me interested in python (becuase I am still in school and I want to focus on getting good grades in computer science) |