Python Forum
[Tkinter] how to add two buttons with different functions in Tk
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] how to add two buttons with different functions in Tk
#1
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()
Yoriz write Mar-26-2022, 02:22 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Attached Files

.py   teliko.py (Size: 2.67 KB / Downloads: 188)
Reply
#2
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.
Reply
#3
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!)
Reply
#4
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()
Reply
#5
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()
Reply
#6
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
Reply
#7
I believe there are 1000 cubic centimeters in a LitreSmile
Reply
#8
That's embarrassing. Did I err in translating the Greek and the conversion is supposed to be liter<->decileter?
Reply
#9
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 Smile ) Unless that means in 100's of cubic centimetres Think
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  2 buttons to run 2 different functions? JP_ROMANO 2 4,210 Jan-10-2019, 02:25 PM
Last Post: JP_ROMANO

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020