0

I try to create a GUI in python and need your help. In this GUI, the user should optimize his results through a choice of options. I wrote the following code:

'''----------------frame Discharge calculation-------------------'''
self.discharge = BooleanVar()
self.discharge.set(False)
self.dischargeBut = Checkbutton(frame, text = "calculate discharge", variable = self.discharge)
self.dischargeBut.place(x=600, y=40)

frame3 = Frame(note)
note.add(frame3, text = "Discharge calculation")
self.xButton = 370
self.xText = 250
self.xText2 = 350
self.yAddText = 10
self.yAddText2 = 10

The Problem is that frame3 should only be visible / active when the Checkbox ("calculate discharge") is selected.

1 Answer 1

0

You can make a widget visible by use of any of the geometry mangers: pack, grid & place. All of these can also unmap a widget so it's not rendered. The easiest way i think would to associate the checkbutton with a callback function which displays or removes the widget.

Be careful though as mapping and unmapping widgets can cause the containing widget to change it's size.

from tkinter import *

root = Tk()
root.geometry('300x200')

'''----------------frame Discharge calculation-------------------'''
discharge = BooleanVar()
discharge.set(False)

def display_calc():
    if discharge.get() == True:
        frame3.place(x=60, y=100)
    else:
        frame3.place_forget()

dischargeBut = Checkbutton(root, text="calculate discharge",
                           variable=discharge, command=display_calc)
dischargeBut.place(x=60, y=40)

# The Discharge calculation widget
frame3 = Frame(root)
Label(frame3, text='Discharge calculation').pack()

root.mainloop()

Also; using place() is almost always a bad choice. Read about grid() and pack() as they are easier to use when your GUI becomes more complicated.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much. It work perfectly. May you have an idea how to make this as a tab. So to make a tab visible. I have to many option for the calculation and the overview of this GUI is very bad.
Please start a new question for follow up questions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.