简体   繁体   中英

Tkinter questionnaire based on get() method

I am trying to make my first GUI script based on the answers of 2 questions. I'll show an example of the non GUI script

while True:
    ammount = input("What is your debt")
    if ammount.isdigit() == True:
        break
    else:
        print("Please try typing in a number")

while True:
    month = input("In which month did the debt originate?")
    if month not in ["January", "February"]:
        print("Try again")
    else:
        break

The point of this script is that it is scalable with all the questions one may add, I want to understand it in the same way in Tkinter. I'll show what I've tried:

from tkinter import *

def click():
        while True:
                entered_text = text_entry.get()
                if entered_text .isdigit() == False:
                        error = Label(window, text="Try again", bg = "black", fg="red", font="none 11").grid(row = 3, column = 2, sticky= W).pack()
                else:
                        break
        return True


window = Tk()

window.title("Tax calculator")
window.configure(background="black")


monto = Label(window, text="¿What is your debt?", bg="black", fg="white", font="none 12 bold")
monto.grid(row = 1, column = 0, sticky= W)

text_entry = Entry(window, width = 20, bg="white")
text_entry.grid(row = 2, column=2, sticky=W)

output = Button(window, text = "Enter", width = 6, command = click)
output.grid(row = 3, column = 0,  sticky = W)

The thing is, I can't add a Label() method in the if/else of the click() method, because I would like to ask a new question once the condition is met. I can't also can't get True from click once the condition is met, because the input comes from Button() method. Thanks in advance

You don't actually need any loops here, a simple if statement would be enough to do the trick. Also, there is no need to recreate the label every time, you can just configure() its text. And, note that indexing starts from 0 - so, in your grid, actual first row (and column) needs to be numbered 0, not 1. Besides that, I suggest you get rid of import * , since you don't know what names that imports. It can replace names you imported earlier, and it makes it very difficult to see where names in your program are supposed to come from. You might want to read what PEP8 says about spaces around keyword arguments , as well:

import tkinter as tk

def click():
    entered_text = text_entry.get()

    if not entered_text.isdigit():
        status_label.configure(text='Please try again')
        text_entry.delete(0, tk.END)
    else:
        status_label.configure(text='Your tax is ' + entered_text)
        text_entry.delete(0, tk.END)

root = tk.Tk()
root.title('Tax calculator')
root.configure(background='black')

monto = tk.Label(root, text='¿What is your debt?', bg='black', fg='white', font='none 12 bold')
monto.grid(row=0, column=0, padx=10, pady=(10,0))

text_entry = tk.Entry(root, width=20, bg='white')
text_entry.grid(row=1, column=0, pady=(10,0))

status_label = tk.Label(root, text='', bg='black', fg='red', font='none 11')
status_label.grid(row=2, column=0)

button = tk.Button(root, text='Enter', width=17, command=click)
button.grid(row=3, column=0, pady=(0,7))

root.mainloop()

I forgot to mention, if your application gets bigger, you might be better off using a class.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM