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
#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


Messages In This Thread
RE: how to add two buttons with different functions in Tk - by deanhystad - Mar-26-2022, 05:36 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  2 buttons to run 2 different functions? JP_ROMANO 2 4,274 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