I am trying to make a Python program that asks the user for a number then reverses it using recursion. My attempt is below, but my code gives me TypeError: unsupported operand type(s) for //: 'Entry' and 'int' - any ideas?
from tkinter import *
def reverseInteger(n, r):
if n==0:
return r
else:
return reverseInteger(n//10, r*10 + n%10)
window = Tk()
window.title("Reverse Integer")
frame1 = Frame(window)
frame1.pack()
number = StringVar()
numEntry = Entry(frame1, textvariable=number)
btGetName = Button(frame1, text = "Calculate", command = reverseInteger(numEntry, 0))
label3 = Label(frame1)
numEntry.grid(row = 1, column = 1)
btGetName.grid(row = 1, column = 2)
label3.grid(row = 2, column = 1, sticky="w")
window.mainloop()
Your recursive function is perfectly fine but there are several other
problems in your code.
The main one is that the command parameter of Button must be the
function that will be called when the user presses on the buttton. In
your code, command is set to the return value of reverseInteger
which is an int. So there is a problem here.
Also it seems to me that you want to put the result of your
calculation in label3 so your StringVar should be attached to it
and not to numEntry.
So here is a version that seems ok to me:
from tkinter import *
def reverseInteger(n, r):
if n==0:
return r
else:
return reverseInteger(n//10, r*10 + n%10)
def reverse(): # called when the user click on the button
value = reverseInteger(int(numEntry.get()), 0)
number.set(value) # change the text of the label
window = Tk()
window.title("Reverse Integer")
frame1 = Frame(window)
frame1.pack()
number = StringVar()
numEntry = Entry(frame1)
btGetName = Button(frame1, text = "Calculate", command = reverse)
label3 = Label(frame1, textvariable=number)
numEntry.grid(row = 1, column = 1)
btGetName.grid(row = 1, column = 2)
label3.grid(row = 2, column = 1, sticky="w")
window.mainloop()
Related
I am new to tkinter and python in general. I am trying to create a window that allows the user to input information (based on Entry and dropdown menus) and based on their choices, some new input will show up which will then be used to calculate the results. I've tried making a minimum reproducible snippet of code, as the original code is quite long.
The problem is that in the buttons I don't understand which command to include so that the program waits for all input before executing the rest of the code. Now it seems that no matter what I include it goes directly to the result part of the code.
I've tried having a function that saves the input and calculates the result as the command for the button, but it still does not wait.
The wait_variable as far as I understood only works for one variable?
import tkinter as tk
root = tk.Tk()
root.geometry("1500x800")
Intro_Label = tk.Label(root, text = "Welcome")
Intro_Label.grid(row=0, column=0)
entry_1 = tk.Entry()
entry_1.insert(0, 2.5) #default value
Label_1 = tk.Label(root, text="Input 1")
Label_1.grid(row=2, column=0)
entry_1.grid(row=2, column=1)
Label_2 = tk.Label(root, text="Input 2 ")
#Options for dropdown menu for transport method
I2_clicked = tk.StringVar()
I2_clicked.set("Choose from dropdown menu")
input2_opt = ["a", "b"]
input2 = tk.OptionMenu( root , I2_clicked , *input2_opt )
Label_3 = tk.Label(root, text="Input 2 ")
#Options for dropdown menu for transport method
I3_clicked = tk.StringVar()
I3_clicked.set("Choose from dropdown menu")
input3_opt = ["x", "y"]
input3 = tk.OptionMenu( root , I3_clicked , *input3_opt )
Label_2.grid(row=3, column=0)
input2.grid(row=3, column=1)
Label_3.grid(row=3, column =3)
input3.grid(row=3, column = 4)
def input_calculations():
first_input = entry_1.get()
if I2_clicked.get() == "a":
if I3_clicked.get() == "x":
entry_4 = tk.Entry()
Label_4 = tk.Label(root, text="Input for x|a")
entry_4.insert(0, 5) #dummy default value
entry_4.grid(row = 4, column = 1 )
Label_4.grid(row = 4, column = 0)
entry_5 = tk.Entry()
Label_5 = tk.Label(root, text="Second input for x|a")
entry_5.insert(0, 4) #dummy default value
entry_5.grid(row = 4, column = 4)
Label_5.grid(row = 4, column = 3)
#wait for both inputs before executing the calculations
save_button = tk.Button(root, text ="Calculate results", command= )
save_button.grid(row = 5, column = 6)
#calculate some results
result = float(entry_4.get())* float(entry_5.get())
elif I3_clicked.get() == "y":
entry_6 = tk.Entry()
Label_6 = tk.Label(root, text="Input for y|a")
entry_6.insert(0, 6) #dummy default value
entry_6.grid(row = 4, column = 1 )
Label_6.grid(row = 4, column = 0)
entry_7 = tk.Entry()
Label_7 = tk.Label(root, text="Second input for y|a")
entry_7.insert(0, 7) #dummy default value
entry_7.grid(row = 4, column = 4)
Label_7.grid(row = 4, column = 3)
entry_8 = tk.Entry()
Label_8 = tk.Label(root, text="Third input for y|a")
entry_8.insert(0, 8) #dummy default value
entry_8.grid(row = 4, column = 6)
Label_8.grid(row = 4, column = 5)
save_button = tk.Button(root, text ="Calculate results", command= )
save_button.grid(row = 5, column = 6)
#wait for input before executing the next lines - what to insert here ??
result = float(entry_6.get()) / float(entry_7.get()) * float(entry_8.get())
#continues for all combinations (b and y, b and x) - different inputs, different calculations for each combo
return result
btn = tk.Button(root, text="Confirm", width=15,command=input_calculations)
btn.grid(row= 10, column= 5)
root.mainloop()
You could link the buttons to functions that do the calculations.
Example function:
def button_pressed():
result = float(entry_6.get()) / float(entry_7.get()) * float(entry_8.get())
You could either pass the entries as arguments in a lambda, or make the whole program inside of a class. This way every method can access all of the widgets.
For your save button you would then need to add the function to the command parameter:
save_button = tk.Button(root, text ="Calculate results", command=button_pressed)
You could make multiple functions / methods if you use a class to do the steps you need.
I am trying to create a standard user ID/PASS login. When I use the next function to check if the entered password and name are right, I always get the "wrong values entered" message. Basically, the variables entry_1 and entry_2 are not storing the input text and I want a solution for that. Maybe any of you guys might propose a solution for that?
I have tried to assign entry_1 and entry_2 to variables but it did'nt work out.
from tkinter import *
root = Tk() # creates a window and initializes the interpreter
root.geometry("500x300")
name = Label(root, text = "Name")
password = Label(root, text = "Password")
entry_1 = Entry(root)
entry_2 = Entry(root)
name.grid(row = 0, column = 0, sticky = E) # for name to be at right use sticky = E (E means east)
entry_1.grid(row = 0, column =1)
x = "Taha"
password.grid(row = 1, column = 0)
entry_2.grid(row = 1, column =1)
y = "123"
c = Checkbutton(root, text = "Keep in logged in").grid(columnspan = 2 ) # mergers the two columns
def next():
if a == entry_1 and b == entry_2:
print ("Proceed")
else:
print("wrong values entered")
def getname():
return name
Next = Button(root, text = "Next", command=next).grid(row = 3, column = 1)
root.mainloop() # keep runing the code
I want the program to return "Proceed" once correct values are entered.
in your code you're not checking for the user input anywhere. You should use get() to return user input. I've modified your code accordingly. Now if you enter Taha as username and 123 as password, you'll get the "Proceed" message.
from tkinter import *
root = Tk() # creates a window and initializes the interpreter
root.geometry("500x300")
name = Label(root, text="Name")
password = Label(root, text="Password")
entry_1 = Entry(root)
entry_2 = Entry(root)
name.grid(row=0, column=0, sticky=E) # for name to be at right use sticky = E (E means east)
entry_1.grid(row=0, column=1)
x = "Taha"
password.grid(row=1, column=0)
entry_2.grid(row=1, column=1)
y = "123"
c = Checkbutton(root, text="Keep in logged in").grid(columnspan=2) # mergers the two columns
def next_window():
user_name = entry_1.get()
user_pass = entry_2.get()
if x == user_name and y == user_pass:
print("Proceed")
else:
print("wrong values entered")
def get_name():
return name
Next = Button(root, text="Next", command=next_window).grid(row=3, column=1)
root.mainloop()
thanks to the people who helped, with your help i could find the missing part in the code. i should have used .get() funtion in order to get the entered text back.
here is the upgraded code with some improvements.
from tkinter import *
from tkinter import messagebox
root = Tk() # creates a window and initializes the interpreter
root.geometry("500x300")
name = Label(root, text = "Name")
password = Label(root, text = "Password")
entry_1 = Entry(root)
entry_2 = Entry(root)
name.grid(row = 0, column = 0, sticky = E) # for name to be at right use sticky = E (E means east)
entry_1.grid(row = 0, column =1)
x = "Taha"
password.grid(row = 1, column = 0)
entry_2.grid(row = 1, column =1)
y = "123"
c = Checkbutton(root, text = "Keep in logged in").grid(columnspan = 2 ) # mergers the two columns
def next():
a = entry_1.get()
b = entry_2.get()
if a == "Taha" and b =="123":
messagebox.showinfo("Login", "successfuly logged in ")
root.destroy()
print ("Proceed")
else:
messagebox.showerror("Error", "wrong values entered")
print("wrong values entered")
root.destroy()
Next = Button(root, text = "Next", command=next).grid(row = 3, column = 1)
root.mainloop() # keep runing the code
I was wondering how to calculate stuff using tkinter buttons. I'm making a simple program to calculate seconds to hours:minutes:seconds. The user inputs an integer using the entry widget on the seconds box and when they press calculate, they get the result via the converted time line. I'm confused on how to start calculating it. I know you get the integer via .get, but I'm stuck on how to do that and calculate it in a h:m:s format. This is my code so far.
import tkinter
from tkinter import *
class TimeConverterUI():
def __init__(self):
self.root_window = Tk()
self.root_window.geometry('400x150')
self.root_window.title('Seconds Converter')
self.text()
self.calculate_button()
self.quit_button()
self.root_window.wait_window()
def text(self):
row_label = tkinter.Label(
master = self.root_window, text = 'Seconds: ')
row_label.grid( row = 0, column = 0, columnspan=2, padx=10, pady=10,
sticky = tkinter.W)
secondsEntry = Entry(master = self.root_window)
secondsEntry.grid(row = 0, column = 1)
row_label = tkinter.Label(
master = self.root_window, text = 'Converted Time(H:M:S): ').grid(row=1)
def calculate_button(self):
quit = Button(self.root_window, text = "Calculate", command = self.calculate)
quit.grid(row = 3, column = 0, columnspan = 3, pady=20,
sticky = tkinter.W)
def calculate(self):
pass
def quit_button(self):
quit = Button(self.root_window, text = "Quit", command = self.quit)
quit.grid(row = 3, column = 3, columnspan = 3, pady=20,
sticky = tkinter.E)
def quit(self) -> bool:
self.root_window.destroy()
return True
if __name__ == '__main__':
convert=TimeConverterUI()
First break this code below into 2 lines if you ever want to use row_label later because this will return NoneType. You should define it first then use .grid on it (just like your button).
row_label = tkinter.Label(
master = self.root_window, text = 'Converted Time(H:M:S): ').grid(row=1)
Now you can create another label to show the result. Remember to put self. before its name so you can use it in the calculate function. Also change secondsEntry to self.secondsEntry for the same reason.Now you just use int(self.secondsEntry.get()) in that function and do the required calculations. Then set the result to that result label with .configure(text=str(result))
I am new in creating GUI. I am doing it in Python with Tkinter. In my program I calculate following characteristics
def my_myfunction():
my code ...
print("Centroid:", centroid_x, centroid_y)
print("Area:", area)
print("Angle:", angle)
I would like to ask for any help/tips how to display those values in GUI window or how to save them in .txt file so that I can call them in my GUI
Thanks in advance
Tkinter is easy and an easy way to do a GUI, but sometimes it can be frustrating. But you should have read the docs before.
However, you can do in this way.
from tkinter import *
yourData = "My text here"
root = Tk()
frame = Frame(root, width=100, height=100)
frame.pack()
lab = Label(frame,text=yourData)
lab.pack()
root.mainloop()
There are several ways to display the results of any operation in tkiner.
You can use Label, Entry, Text, or even pop up messages boxes. There are some other options but these will probably be what you are looking for.
Take a look at the below example.
I have a simple adding program that will take 2 numbers and add them together. It will display the results in each kind of field you can use as an output in tkinter.
import tkinter as tk
from tkinter import messagebox
class App(tk.Frame):
def __init__(self, master):
self.master = master
lbl1 = tk.Label(self.master, text = "Enter 2 numbers to be added \ntogether and click submit")
lbl1.grid(row = 0, column = 0, columnspan = 3)
self.entry1 = tk.Entry(self.master, width = 5)
self.entry1.grid(row = 1, column = 0)
self.lbl2 = tk.Label(self.master, text = "+")
self.lbl2.grid(row = 1, column = 1)
self.entry2 = tk.Entry(self.master, width = 5)
self.entry2.grid(row = 1, column = 2)
btn1 = tk.Button(self.master, text = "Submit", command = self.add_numbers)
btn1.grid(row = 2, column = 1)
self.lbl3 = tk.Label(self.master, text = "Sum = ")
self.lbl3.grid(row = 3, column = 1)
self.entry3 = tk.Entry(self.master, width = 10)
self.entry3.grid(row = 4, column = 1)
self.text1 = tk.Text(self.master, height = 1, width = 10)
self.text1.grid(row = 5, column = 1)
def add_numbers(self):
x = self.entry1.get()
y = self.entry2.get()
if x != "" and y != "":
sumxy = int(x) + int(y)
self.lbl3.config(text = "Sum = {}".format(sumxy))
self.entry3.delete(0, "end")
self.entry3.insert(0, sumxy)
self.text1.delete(1.0, "end")
self.text1.insert(1.0, sumxy)
messagebox.showinfo("Sum of {} and {}".format(x,y),
"Sum of {} and {} = {}".format(x, y, sumxy))
if __name__ == "__main__":
root = tk.Tk()
myapp = App(root)
root.mainloop()
import sys
from tkinter import *
def printer():
print(message)
print(offset)
gui = Tk()
gui.title("Caesar Cypher Encoder")
Button(gui, text="Encode", command=printer).grid(row = 2, column = 2)
Label(gui, text = "Message").grid(row = 1, column =0)
Label(gui, text = "Offset").grid(row = 1, column =1)
message = Entry(gui)
message.grid(row=2, column=0)
offset = Scale(gui, from_=0, to=25)
offset.grid(row=2, column=1)
mainloop( )
When i run the above code with an input in both the input box and a value on the slider - it comes up with the ouput
.46329264
.46329296
How would i get it to display the string inputted into the text box, and the value selected on the slider
Use Entry.get, Scale.get methods:
def printer():
print(message.get())
print(offset.get())