简体   繁体   English

基于get()方法的Tkinter问卷

[英]Tkinter questionnaire based on get() method

I am trying to make my first GUI script based on the answers of 2 questions. 我正在尝试根据2个问题的答案制作第一个GUI脚本。 I'll show an example of the non GUI script 我将展示非GUI脚本的示例

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. 该脚本的要点是,它可以扩展所有问题,我想在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. 问题是,我无法在click()方法的if / else中添加Label()方法,因为一旦满足条件,我想问一个新问题。 I can't also can't get True from click once the condition is met, because the input comes from Button() method. 一旦满足条件,我也无法从单击中获得True ,因为输入来自Button()方法。 Thanks in advance 提前致谢

You don't actually need any loops here, a simple if statement would be enough to do the trick. 实际上,您实际上不需要任何循环,一个简单的if语句就足以解决问题。 Also, there is no need to recreate the label every time, you can just configure() its text. 另外,不需要每次都重新创建标签,您只需configure()它的文本即可。 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. 并且,请注意,索引从0开始-因此,在网格中,实际的第一行(和列)需要编号为0,而不是1。此外,我建议您摆脱import * ,因为您不知道什么导入的名称。 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: 您可能还想阅读PEP8关于关键字参数周围空格的内容

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. 我忘了提一下,如果您的应用程序变大,使用类可能会更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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