I made a calculator with tkinter(obviously in python) and it is meant to ask for user input, and depending on the input, show a result on the screen. However, it doesnt wait for input and just prints the result instead!
Heres the code:
#import modules
import tkinter as tk
import os
import time
window = tk.Tk()
end = window.mainloop
header= tk.Label(
text = "Calculator",
fg = "black",
bg = "white",
)
header.pack()
entry = tk.Entry()
label = tk.Label(text="Enter your first number:")
label.pack()
entry.pack()
ans1 = entry.get()
time.sleep(1)
label2 = tk.Label(text="Enter your second number:")
label2.pack()
entry2 = tk.Entry()
entry2.pack()
ans2 = entry2.get()
time.sleep(1)
label3 = tk.Label(text="Enter your operator:")
label3.pack()
entry3 = tk.Entry()
entry3.pack()
ans3 = entry3.get()
time.sleep(10)
if ans3 == "add":
result = ans1 + ans2
elif ans3 == "sub":
result = ans1 - ans2
elif ans3 == "mul":
result = ans1 * ans2
elif ans3 == "div":
result = ans1 / ans2
elif ans3 == "mod":
result = ans1 % ans2
elif ans3 == "pow":
result = ans1 ** ans2
else:
result = "error"
label4 = tk.Label(text="The result is:")
label4.pack()
label5 = tk.Label(text=result)
label5.pack()
end()
I made a calculator with tkinter(obviously in python) and it is meant to ask for user input, and depending on the input, show a result on the screen. However, it doesnt wait for input and just prints the result instead!
Heres the code:
#import modules
import tkinter as tk
import os
import time
window = tk.Tk()
end = window.mainloop
header= tk.Label(
text = "Calculator",
fg = "black",
bg = "white",
)
header.pack()
entry = tk.Entry()
label = tk.Label(text="Enter your first number:")
label.pack()
entry.pack()
ans1 = entry.get()
time.sleep(1)
label2 = tk.Label(text="Enter your second number:")
label2.pack()
entry2 = tk.Entry()
entry2.pack()
ans2 = entry2.get()
time.sleep(1)
label3 = tk.Label(text="Enter your operator:")
label3.pack()
entry3 = tk.Entry()
entry3.pack()
ans3 = entry3.get()
time.sleep(10)
if ans3 == "add":
result = ans1 + ans2
elif ans3 == "sub":
result = ans1 - ans2
elif ans3 == "mul":
result = ans1 * ans2
elif ans3 == "div":
result = ans1 / ans2
elif ans3 == "mod":
result = ans1 % ans2
elif ans3 == "pow":
result = ans1 ** ans2
else:
result = "error"
label4 = tk.Label(text="The result is:")
label4.pack()
label5 = tk.Label(text=result)
label5.pack()
end()
I have tried using time.sleep()
to make it wait for input and then show the result. but it just waits before opening the program GUI.
Xbox YT is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.