I am working on a GUI with tkinter module. When I run my script, I have two widgets (entry and radiobutton) and a pushbutton to close the window. The size of the window is adjusted to the widgets. If the user selects yes at the first question, two other widget appear and user still have to select yes or no. As previously, if yes is selected, another widget appears below. So by selecting yes, the height of the windows changes and I'd like to let the pusbutton at the bottom by keeping the window size adjusted to the widget. How could I do that ? Thanks in advance for your help
from tkinter import *
import tkinter as tk
class Application(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.c2 = tk.Button(self,
text='Quit',
fg='White',
bg= 'dark green',height = 1, width = 10,command=self.destroy)
self.c2.grid(ipadx = 100)
self.c2.place(x=200,y=370)
self.txt1 = tk.Label(self, text = 'Prescribed dose :')
self.txt1.place(x=40, y=20)
self.e1 = tk.Entry(self, textvariable = ' ',width=10,fg="blue",bd=3)
self.e1.place(x=300, y= 20)
self.txt3 = tk.Label(self, text = 'Is there another target ?')
self.txt3.place(x=40,y=70)
self.radioValueOuiNonPourBoostPresent = tk.IntVar()
self.rdioOuiBoostPresent = tk.Radiobutton(self, text='Yes', variable=self.radioValueOuiNonPourBoostPresent, value=1, command = self.create_firstWidget)
self.rdioOuiBoostPresent.place(x=290, y=70)
self.rdioNonBoostPresent = tk.Radiobutton(self, text='No', variable=self.radioValueOuiNonPourBoostPresent, value=2, command = self.hide_selection)
self.rdioNonBoostPresent.grid(column=3, row=3, pady = 10)
self.rdioNonBoostPresent.place(x=350, y=70)
def create_firstWidget(self):
if self.radioValueOuiNonPourBoostPresent.get() == 1:
self.txt5 = tk.Label(self, text = 'Prescribed dose of the 2nd target :')
self.txt5.place(x = 40 , y = 120)
self.doseBoost = tk.StringVar(self)
self.entry1 = tk.Entry (self, textvariable = self.doseBoost, width=10 ,fg="blue" ,bd=3)
self.entry1.place(x=300 , y = 120)
self.txt6 = tk.Label(self, text = 'Is it a sequential scheme ?')
self.txt6.place(x = 40 ,y =170)
self.rdioOuiNonSequentiel = tk.IntVar()
self.rdioOuiSequentiel = tk.Radiobutton(self, text='Yes', variable=self.rdioOuiNonSequentiel, value=1, command = self.create_secondtWidget)
self.rdioNonSequentiel = tk.Radiobutton(self, text='No', variable=self.rdioOuiNonSequentiel, value=2, command = self.hide_selection2)
self.rdioOuiSequentiel.place(x=290, y=170)
self.rdioNonSequentiel.place(x=350, y=170)
def create_secondtWidget(self):
if self.rdioOuiNonSequentiel.get()==1:
self.txt7 = tk.Label(self, text = 'Number of fractions')
self.txt7.place(x = 40 , y = 220)
self.nombreFractionsBoost = tk.StringVar(self)
self.entry2 = tk.Entry (self, textvariable = self.nombreFractionsBoost, width=10 ,fg="blue" ,bd=3)
self.entry2.place(x=300 , y = 220)
def hide_selection(self):
if self.radioValueOuiNonPourBoostPresent.get() == 2:
if self.entry1.winfo_exists():
self.txt5.place_forget()
self.txt6.place_forget()
self.rdioOuiSequentiel.place_forget()
self.rdioNonSequentiel.place_forget()
self.entry1.place_forget()
if self.entry2.winfo_exists():
self.txt7.place_forget()
self.entry2.place_forget()
def hide_selection2(self):
if self.rdioOuiNonSequentiel.get() == 2:
self.txt7.place_forget()
self.entry2.place_forget()
if __name__ == "__main__":
app = Application()
app.title('test')
app.geometry("500x400")
app.mainloop()
CodePudding user response:
From what is understood (as the minimal reproducible example isn't given) you want to add widgets and resize the window when a particular event is passed.
As an example, let's take a function that you enter when a user selects any given option:
root = Tk()
root.geometry('400x400')
def myfunction(answer):
if answer == 'yes':
root.geometry('400x600)
#Your code here
Here you are packing all widgets at the bottom but since the root isn't as big, they don't show. Later, you increase the root's size and it will work.
Another method (a better one in my opinion) is to simply pack the widgets when needed but define them earlier. This method is less shabby.
Thank You!