Posts: 3
Threads: 1
Joined: Mar 2022
Mar-26-2022, 02:16 PM
(This post was last modified: Mar-26-2022, 04:49 PM by LinuxLife.
Edit Reason: Added prefix
)
hello all!
first time here,to ask for your support...
got this code in my attached file.it has two buttons,and two different functions.
the problem is that it does not run....!
if i remove the one block or the other,works fine,but not all together....
pls take a look,and advice.....thank you all!
here is my code:
import tkinter as tk
def main():
window= tk.Tk()
window.title("ΕΚΑΤΟΣΤΑ to ΛΙΤΡΑ Converter")
window.geometry("675x400")
# block 1 - the first question
# create a label with text Enter ΕΚΑΤΟΣΤΑ
label1 = tk.Label(window, text="ΠΟΣΑ ΕΚΑΤΟΣΤΑ ΕΧΕΙΣ:")
# create a label with text ΛΙΤΡΑ:
label2 = tk.Label(window, text="ΕΧΕΙΣ ΛΙΤΡΑ:")
# place label1 in window at position x,y
label1.place(x=50,y=30)
# create an Entry widget (text box)
textbox1 = tk.Entry(window, width=12)
# place textbox1 in window at position x,y
textbox1.place(x=200,y=35)
# create an Entry widget (text box)
textbox2 = tk.Entry(window, width=12)
# place label2 in window at position x,y
label2.place(x=50,y=100)
# create a label3 with empty text:
label3 = tk.Label(window, text=" ")
# place label3 in window at position x,y
label3.place(x=180,y=100)
def btn1_click():
ΛΙΤΡΑ = round(float(textbox1.get()) * 10)
label3.configure(text = str(ΛΙΤΡΑ)+ ' ΛΙΤΡΑ')
# create a button with text Button 1
btn1 = tk.Button(window, text="ΠΑΤΗΣΕ ΓΙΑ ΝΑ ΒΡΕΙΣ ΤΑ ΛΙΤΡΑ", command=btn1_click)
# place this button in window at position x,y
btn1.place(x=90,y=150)
window.mainloop()
# block 2 - LITRA SE EKATOSTA ===========
# create a label with text Enter ΛΙΤΡΑ
label4 = tk.Label(window, text="ΠΟΣΑ ΛΙΤΡΑ ΠΑΡΑΓΓΕΙΛΕΣ: ")
# create a label with text ΛΙΤΡΑ:
label5 = tk.Label(window, text="ΤΑ ΛΙΤΡΑ ΑΥΤΑ ΕΙΝΑΙ :")
# place label2 in window at position x,y
label5.place(x=50,y=600)
# place label1 in window at position x,y
label4.place(x=50,y=530)
# create an Entry widget (text box)
textbox2 = tk.Entry(window, width=12)
# place textbox1 in window at position x,y
textbox2.place(x=200,y=530)
# create a label3 with empty text:
label6 = tk.Label(window, text=" ")
# place label3 in window at position x,y
label6.place(x=180,y=600)
def btn2_click():
ΛΙΤΡΑ = round(float(textbox2.get()) / 10)
label6.configure(text = str(ΛΙΤΡΑ)+ ' ΕΚΑΤΟΣΤΑ')
# create a button with text Button 2
btn2 = tk.Button(window, text="ΠΑΤΗΣΕ ΓΙΑ ΝΑ ΒΡΕΙΣ ΤΑ ΕΚΑΤΟΣΤΑ", command=btn2_click)
# place this button in window at position x,y
btn2.place(x=90,y=650)
window.mainloop()
# end of block 2 =====================================
main()
Attached Files
teliko.py (Size: 2.67 KB / Downloads: 328)
Posts: 6,806
Threads: 20
Joined: Feb 2020
Mar-26-2022, 04:28 PM
(This post was last modified: Mar-26-2022, 04:48 PM by deanhystad.)
You are reusing variable names.
textbox2 is an entry in line 22 and a different entry in line 68. Line 68 executes last, so textbox2 is the second entry. When btn2_click executes, it gets text from the second entry, not the first. I also don't see any code that places the first textbox2.
There can be only one mainloop() in your program. After you make everything you call mainloop(). mainloop() will block the program until the root window is closed.
Posts: 3
Threads: 1
Joined: Mar 2022
Mar-26-2022, 04:43 PM
(This post was last modified: Mar-26-2022, 04:44 PM by LinuxLife.)
well,i tried this aproach...no luck.....
what i need,is to get two inputs,from two different variables (i input centimeters and get liters and vice versa)
it only shows one button and its fields....
any other idea?
just a note,i use python 3 (if it matters!)
Posts: 6,806
Threads: 20
Joined: Feb 2020
Mar-26-2022, 05:36 PM
(This post was last modified: Mar-28-2022, 05:06 PM by deanhystad.)
Your window is too small. You cannot see the other widgets. You should not be using place() or setting the geometry of the window. Use pack() or grid() and let tkinter size the window to fit all the widgets.
I would write your code like this:
import tkinter as tk
class MainWindow(tk.Tk):
def __init__(self):
super().__init__()
self.title("Converter")
tk.Label(self, text="Cubic Centimeter to Liter Converter:") \
.grid(row=0, column=0, columnspan=5, padx=5, pady=5)
self.liters = tk.DoubleVar(value=0.0)
tk.Entry(self, textvariable=self.liters, width=12, justify=tk.RIGHT) \
.grid(row=1, column=0, padx=5, pady=5)
tk.Label(self, text="L").grid(row=1, column=1, sticky="w")
tk.Button(self, text="<<", command=self.convert_to_liters) \
.grid(row=1, column=2, padx=5, pady=5)
tk.Button(self, text=">>", command=self.convert_to_cm3) \
.grid(row=1, column=3, padx=5, pady=5)
self.cm3 = tk.DoubleVar(value=0.0)
tk.Entry(self, textvariable=self.cm3, width=12, justify=tk.RIGHT) \
.grid(row=1, column=4, padx=5, pady=5)
tk.Label(self, text="cm3").grid(row=1, column=5, sticky="w")
def convert_to_cm3(self):
self.cm3.set(self.liters.get() * 10)
def convert_to_liters(self):
self.liters.set(self.cm3.get() / 10)
MainWindow().mainloop() I used tkinter variables for to make it easier to get and set the textboxes. I also used more meaningful names so the program logic is easer to follow. textbox1 and textbox2 do not tell me that one is for entering liters and the other cubic centimeters.
For things like buttons and labels, if you don't change it, don't assign it to a variable. The only variables I need in my program are self.liters and self.cm3 because these are the only things I need to change once the window is created.
Instead of changing a label I think it makes more sense to change the textboxes. If you press the convert to liters button it reads the cubic centimeters, converts to liters, and set liters textbox to display the new value. For liters to cubic centimeters read the liters, convert to cubic centimeters, update the cubic centimeters textbox.
I also wrote the window as a class. This is my preference for writing tkinter code. It really helps when you make a program with multiple windows. Each window has it's own clearly defined code. You can even break the program up into multiple modules.
For a simple program like this you don't get a lot of benefit from classes. This is my code written without a class.
import tkinter as tk
def convert_to_cm3():
cm3.set(liters.get() * 10)
def convert_to_liters():
liters.set(cm3.get() / 10)
root = tk.Tk()
root.title("Converter")
tk.Label(root, text="Cubic Centimeter to Liter Converter:") \
.grid(row=0, column=0, columnspan=5, padx=5, pady=5)
liters = tk.DoubleVar(value=0.0)
tk.Entry(root, textvariable=liters, width=12, justify=tk.RIGHT) \
.grid(row=1, column=0, padx=5, pady=5)
tk.Label(root, text="L").grid(row=1, column=1, sticky="w")
tk.Button(root, text="<<", command=convert_to_liters) \
.grid(row=1, column=2, padx=5, pady=5)
tk.Button(root, text=">>", command=convert_to_cm3) \
.grid(row=1, column=3, padx=5, pady=5)
cm3 = tk.DoubleVar(value=0.0)
tk.Entry(root, textvariable=cm3, width=12, justify=tk.RIGHT) \
.grid(row=1, column=4, padx=5, pady=5)
tk.Label(root, text="cm3").grid(row=1, column=5, sticky="w")
root.mainloop()
Posts: 3
Threads: 1
Joined: Mar 2022
well...thank you all for your help!! finally got it ! your tips helped me a lot!
would like to make an addition though...
as you can see,i get two values from my inputs (final centimeters and final liters)
is there any way to add a button that sums up some input that i will give in centimeters and the already calculated centimeters output?
thanks again!
(i attach the final code below)
import tkinter as tk
def main():
window= tk.Tk()
window.title(" ERRIKOS Converter")
window.geometry("800x800")
# create a label with text Enter centimetre
label1 = tk.Label(window, text="gimme the centimetres: ")
# create a label with text litres
label2 = tk.Label(window, text="we have litres:")
# place label1 in window at position x,y
label1.place(x=50,y=30)
# create an Entry widget (text box)
textbox1 = tk.Entry(window, width=12)
# place textbox1 in window at position x,y
textbox1.place(x=200,y=35)
# place label2 in window at position x,y
label2.place(x=50,y=100)
# create a label3 with empty text:
label3 = tk.Label(window, text=" ")
# place label3 in window at position x,y
label3.place(x=180,y=100)
def btn1_click():
litres = round(float(textbox1.get()) * 10)
label3.configure(text = str(litres)+ ' litres')
# create a button with text Button 1
btn1 = tk.Button(window, text="print litres", command=btn1_click)
# place this button in window at position x,y
btn1.place(x=90,y=150)
# create a label with text Enter litres
label4 = tk.Label(window, text="ordered litres: ")
# create a label with text centimetres
label5 = tk.Label(window, text="litres in centimetres: ")
# place label4 in window at position x,y
label4.place(x=50,y=730)
# create an Entry widget (text box)
textbox2 = tk.Entry(window, width=12)
# place textbox2 in window at position x,y
textbox2.place(x=200,y=735)
# place label5 in window at position x,y
label5.place(x=50,y=700)
# create a label6 with empty text:
label6 = tk.Label(window, text=" ")
# place label6 in window at position x,y
label6.place(x=180,y=700)
def btn2_click():
centimetres = round(float(textbox2.get()) / 10)
label6.configure(text = str(centimetres)+ ' centimetres')
# create a button with text Button 2
btn2 = tk.Button(window, text="print centimetres", command=btn2_click)
# place this button in window at position x,y
btn2.place(x=90,y=750)
window.mainloop()
main()
Posts: 6,806
Threads: 20
Joined: Feb 2020
Quote:is there any way to add a button that sums up some input that i will give in centimeters and the already calculated centimeters output?
yes
Posts: 163
Threads: 13
Joined: Oct 2016
I believe there are 1000 cubic centimeters in a Litre
Posts: 6,806
Threads: 20
Joined: Feb 2020
That's embarrassing. Did I err in translating the Greek and the conversion is supposed to be liter<->decileter?
Posts: 163
Threads: 13
Joined: Oct 2016
Mar-28-2022, 04:59 PM
(This post was last modified: Mar-28-2022, 04:59 PM by Barrowman.)
I don't know Greek but Google Translate says
LITRA SE EKATOSTA
Means
LITERS IN HUNDREDS
So it's neither decilitres or cubic centimetres ( In GB English  ) Unless that means in 100's of cubic centimetres
|