问题Tkinter:如何正确调整窗口大小?

2024-10-02 22:37:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在学习Python Tkinter,所以我制作了一个Pomotoro计时器。它具有如下展开和收缩模式: Expand Mode

Shrink Mode

右下角的按钮在模式之间切换。 默认情况下,窗口以展开模式打开。当我第一次收缩时,它收缩了原本应该收缩的部分。然后我切换回展开模式后,如果我收缩它,它会变成这样:

enter image description here . 以下是我的心理咨询方法:

def shrink():
    """To generate the minimized window"""
    root.resizable(0,0) #Making the window un-resizable
    root.call('wm', 'attributes', '.', '-topmost', True) #To make the window stick to the top
    root.overrideredirect(True) #To hide the title bar
    root.bind("<Button-1>",savelastclickpos)
    root.bind("<B1-Motion>",dragging)
    root['cursor']='fleur'
    img_lbl.grid_forget() #To remove the big image
    title_lbl.grid_forget() #To remove the big title 
    shr_btn.grid_forget() #To remove the shrink button
    root.geometry('400x60') #Changing the dimensions of the window
    global tmt_lbl
    global btn_exp
#-----------------New smaller image------------------#
    tmt_img=PhotoImage(file='./tomato_40x40.png')
    tmt_lbl=Label(image=tmt_img,bg='#ffffff')
    tmt_lbl.image=tmt_img
#----------------------------------------#
    tmt_lbl.grid(row=0,column=0,padx=5,sticky='w')
    timer_lbl.grid(row=0,column=1,columnspan=1)
    btn_frame.grid(row=0,column=2,pady=5,sticky='w')
    btn_exp=Button(image=exp_img,command=expand)
    btn_exp.grid(row=0,column=3,sticky='se')
    btn_exp['cursor']='hand2'

以下是我的展开方法:

def expand():
    """To manage the maximized window"""
    root.resizable(True,True) #Making the window resiable again
    root.call('wm', 'attributes', '.', '-topmost', False) #Allowing the window to be hidden
    root.overrideredirect(False) #Making the title bar visible again
    root['cursor']='arrow'
    tmt_lbl.destroy() #Removing the smaller label
    btn_exp.destroy() #Removing the expand button to replace it with the shrink button
    root.geometry('450x310') #Changing the dimensions of the window
    img_lbl=Label(bg='#ffffff')
    img_lbl.image=img
    img_lbl['image']=img
    img_lbl.grid(row=0,column=0)
    title_lbl=Label(text='Pomodoro\nTimer',font=myfont,fg='black',bg='#ffffff')
    title_lbl.grid(row=0,column=1,columnspan=2)
    timer_lbl.grid(row=1,column=0,columnspan=3)
    btn_frame.grid(row=2,column=0,columnspan=3,sticky='')
    shr_btn=Button(image=shr_img,bd=0,bg='#ffffff',command=shrink)
    shr_btn['cursor']='hand2'
    shr_btn.grid(row=2,column=2,sticky='se',padx=5,pady=5)

我无法理解,如果它在第一次时完美无瑕,那么为什么第二次尝试时会一团糟


Tags: thetoimageimgtitlecolumnrootwindow