Python Forum
[Tkinter] .get() doesn't exist?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] .get() doesn't exist?
#1
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!
Reply
#2
try

Box=Entry(window,width=15)
Box.grid(row=2, column=3,sticky=E)
Reply
#3
(Oct-13-2019, 03:52 PM)Axel_Erfurt Wrote: try

Box=Entry(window,width=15)
Box.grid(row=2, column=3,sticky=E)

The Entry shows up, the issue I am having is the fact that I cant get the data from that entry
Reply
#4
What I write in the box I get as output, what should happen with it?
Reply
#5
(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
Reply
#6
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.
Reply
#7
(Oct-13-2019, 10:11 PM)joe_momma Wrote: 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.

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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] _tkinter.TclError: image "pyimage2" doesn't exist Killdoz 1 10,511 May-30-2020, 09:48 AM
Last Post: menator01

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020