Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 问题Tkinter:如何正确调整窗口大小?_Python_Python 3.x_Tkinter - Fatal编程技术网

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

Python 问题Tkinter:如何正确调整窗口大小?,python,python-3.x,tkinter,Python,Python 3.x,Tkinter,我正在学习Python Tkinter,所以我制作了一个Pomotoro计时器。它具有如下展开和收缩模式: 右下角的按钮在模式之间切换。 默认情况下,窗口以展开模式打开。当我第一次收缩时,它收缩了原本应该收缩的部分。然后我切换回展开模式后,如果我收缩它,它会变成这样: . 以下是我的心理咨询方法: def shrink(): """To generate the minimized window""" root.r

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

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

. 以下是我的心理咨询方法:

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'

我无法理解,如果它在第一次完美地发生,那么为什么在第二次尝试时会出错?

我看到您在
展开
函数中创建了一个新的
img\u lbl
title\u lbl
shr\u btn
,然后在
收缩
函数中引用它们。在函数中创建变量/对象将使其成为该函数的局部变量。我猜当您第一次在全局范围内初始化这些对象时。然后在展开后,销毁它们,并仅在本地范围内初始化它们。这将导致您对
shrink
的第二次调用的行为与第一次调用不同。@Axe319那么,我是否应该全局创建它们一次,然后我可以
pack
pack\u忘记它们?是的,这是我的建议。一旦您了解了类和共享状态,您可以清理一些东西,但暂时可以让它正常工作。
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)