2

I'm working on a GUI Python program using Tkinter.

I have a function that is called when a button is pressed (and when the program is loaded). The program is currently unfinished and only checks data validation at this current point. As the default entry is current invalid, it throws an error.

However, after this point, the entry box is disabled and will not let me enter any data. I cannot figure out why this is happening and I was wondering if someone could tell me the reason so I can work on a solution.

Thanks

import sys
import random
from tkinter import *
from tkinter import ttk
from tkinter import messagebox

root = Tk()
root.title("COSC110 - Guessing Game")
hint = StringVar()
guesses = []
guess_input = ''

def loadWordList(filename): #Load the words from a file into a list given a filename.
    file = open(filename, 'r')
    line = file.read().lower()
    wordlist = line.split()

    return wordlist

word = random.choice(loadWordList('words.txt'))

def getHint(word, guesses): #Get hint function, calculates and returns the current hint.
    hint = ' '
    for letter in word:
        if letter not in guesses:
            hint += '_ '
        else:
            hint += letter

    return hint

def guessButton(guess, word, guesses):
    guess = str(guess_input)
    guess = guess.lower()

    if not guess.isalpha():
        is_valid = False
    elif len(guess) !=1:
        is_valid = False
    else:
        is_valid = True

    while is_valid == False:
        messagebox.showinfo("Error:","Invalid input. Please enter a letter from a-z.")
        break

    hint.set(getHint(word, guesses))
    return hint

label_instruct = Label(root, text="Please enter your guess: ")
label_instruct.grid(row=1,column=1,padx=5,pady=10)

guess_input = Entry(root,textvariable=guess_input)
guess_input.grid(row=1, column=2)

guess_button = Button(root, text="Guess", width=15, command=guessButton(guess_input,word,guesses))
guess_button.grid(row=1, column=3,padx=15)

current_hint = Label(root, textvariable=hint)
current_hint.grid(column=2,row=2)

label_hint = Label(root, text="Current hint:")
label_hint.grid(column=1,row=2)

label_remaining = Label(root, text="Remaining guesses: ")
label_remaining.grid(column=1,row=3)

root.mainloop() # the window is now displayed

Any tips are appreciated.

1 Answers1

0

There are two apparent problems.

Firstly, you shouldn't use

guess_button = Button(root, text="Guess", width=15, command=guessButton(guess_input,word,guesses))

because you can't call a function with arguments on the command config.

My suggestion would be to take a look here and use one of the proposed methods, I particularly like the one using functools and partial:

from functools import partial
#(...)
button = Tk.Button(master=frame, text='press', command=partial(action, arg))

with action being the function you want to call and arg the parameters you want to call separated by a comma.

Secondly, you are using

guess = str(guess_input)

which doesn't return the Entry typed text, use instead

guess = guess_input.get()

PS: Albeit not directly related to your question, you should use

if var is False:

instead of

if var == False:
KenHBS
  • 6,756
  • 6
  • 37
  • 52