如何始终将标签置于彼此之上

问题描述 投票:0回答:1

在我的代码中,有一些复选框可以选择打印的数据。但如果我使用 .place 并取消选中某些框,显示之间就会出现间隙。例如:

但是,如果我使用 pack(),代码将卡在窗口顶部。例如:

我应该怎样做才能使它们始终位于彼此之上,同时又居中。

python tkinter
1个回答
0
投票
from tkinter import *

#functions

def get_distance_travelled():
    # your code to find the distance travelled. I have given a dummy value
    return 15 #Remove this line and change according to yours
    
def get_max_height():
    #your code to find max_height. I have given a dummy value
    return 100 #Remove this line and change according to yours

def get_final_velocity():
    # your code to get final velocity. I have given a dummy value
    return 1000 #Remove this line and change according to yours
    
def get_flight_time():
    # your code to get flight_time.I have given a dummy value
    return '10:10' #Remove this line and change according to yours
    
def show_items():
    pop = Toplevel(root)
    pop.geometry('300x200')
    fn_lst = [get_distance_travelled(), get_max_height(), get_final_velocity(), get_flight_time() ]
    for i in range(len(ls)):
        if vardct[i].get()==1:
            Label(pop, text=f'{keydct[i]} : {fn_lst[i]}').pack()

#design

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


# list containing display texts
ls =    ['Display Distance Travelled', 
         'Display Max Height',
        'Display Final Velocity',
        'Display Flight Name']
        
        
# Create 3 dicts say for display texts, intvars, and wdgts
keydct, vardct, wdgdct = {}, {}, {}

#create widgets and pack
for i in range(len(ls)):
    keydct[i] = ls[i]
    vardct[i] = IntVar()
    wdgdct[i] = Checkbutton(root, text = keydct[i], variable = vardct[i])
    wdgdct[i].pack() 


    
     
    
btn = Button(root, text = 'Show', command=show_items)
btn.pack()
root.mainloop()
© www.soinside.com 2019 - 2024. All rights reserved.