New to python so this is probably a dumb question...
I want to say that if text1.get() == "1" then x = 0
and if text2.get() = "2" then y = 8
. Then I want a button
that adds x
and y
.
from tkinter import *
new = Tk()
new.title("Lockdown?")
new.geometry("700x400")
text3=Entry(new, width = "60")
text3.place(rely=0.15)
if text3.get() == "":
label9 = Label(new, text="required field", fg="red")
label9.place(rely=0.17)
elif text3.get() == "1":
x = "0"
elif text3.get() == "2":
x = "1"
elif text3.get() == "10":
x = "9"
else:
label10 = Label(new, text = "Please use a number ranging from 1-10")
text4=Entry(new, width = "60")
text4.place(rely=0.26)
if text4.get() == "":
label11 = Label(new, text="Required Field", fg = "red")
label11.place(rely=0.19)
elif text4.get() == "7":
y = "3"
elif text4.get() == "8":
y = "2"
elif text4.get() == "9":
y = "1"
elif text4.get() == "10":
y = "0"
else:
label11 = Label(new, text = "Please use a number ranging from 1-10")
def cmd3():
label15 = Label(new, text = x + y)
label15.place(rely=0.8)
btn3 = Button(new, text = "Submit Answers", command = cmd3, font=("Arial Bold", 25), bg = "white", fg = "black")
btn3.place(rely=0.71)
new.mainloop()
GUI frameworks ( tkinter
, PyQt
, wxPython
, etc., and in other languages) don't work like function input()
. Entry
doesn't wait for your data - it only inform mainloop
what it has to display in window
. And mainloop
starts all - it displays window, etc. If you want to get value from Entry
then you should do it in function assigned to Button
.
My version with other changes
I use pack()
instead of place()
because it is simpler to organize widgets.
I create all labels at start (with empty text) and later I replace text in labels.
import tkinter as tk # PEP8: `import *` is not preferred
# --- functions ---
def cmd3():
# default value at start
x = None
y = None
# remove previous text
label1['text'] = ""
label2['text'] = ""
text = text1.get()
if text == "":
label1['text'] = "required field"
elif text == "1":
x = 0
elif text == "2":
x = 1
elif text == "10":
x = 9
else:
label1['text'] = "Please use a number ranging from 1-10"
text = text2.get()
if text == "":
label2['text'] = "required field"
elif text == "7":
y = 3
elif text == "8":
y = 2
elif text == "9":
y = 1
elif text == "10":
y = 0
else:
label2['text'] = "Please use a number ranging from 1-10"
print(x, y)
# it can't be `if x and y:` because `0` will gives `False`
if (x is not None) and (y is not None):
label_result['text'] = str(x + y)
# --- main ---
new = tk.Tk()
# empty at start
label1 = tk.Label(new, text="", fg="red")
label1.pack()
text1 = tk.Entry(new, width="60") # PEP8: arguments without spaces around `=`
text1.pack()
# empty at start
label2 = tk.Label(new, text="", fg="red")
label2.pack()
text2 = tk.Entry(new, width="60")
text2.pack()
btn3 = tk.Button(new, text="Submit Answers", command=cmd3)
btn3.pack()
# empty at start
label_result = tk.Label(new, text="", fg="green")
label_result.pack()
new.mainloop()
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.