代码之家  ›  专栏  ›  技术社区  ›  MacSell

当复合选项从无重新配置为任何其他值时,Tkinter按钮高度发生变化

  •  0
  • MacSell  · 技术社区  · 2 年前

    我试图在我的应用程序中添加几个带有图像和文本的按钮。默认情况下,通过设置,所有按钮都具有隐藏文本:

    compound=tk.NONE

    例如,当按下一个特定按钮时,复合值会切换到任何其他值

    compound=tk.LEFT

    然后可以观察到按钮高度的变化。我不明白为什么。也许这是正常的行为,或者更可能的情况——也许是我的错,需要一些额外的/不同的设置。 我读到,当按钮指定了图像和文本时,其大小输入值将被处理为像素,而不是字体字符大小。在我的情况下,这是真的,检查了字体大小不会影响按钮高度。

    我准备了一个简单的应用程序来解决这个问题:

    import tkinter as tk
    
    class psWindow():
        def __init__(self, parent) -> None:
            self.parent = parent
           
            #Main window geometry
            self.setParentGeometry()
    
            #Parent grid weight
            self.parent.grid_columnconfigure(0,weight=1)
            self.parent.grid_rowconfigure(0,weight=1)
    
            #Frame 1, spread to parent size
            self.frame1 = tk.Frame(parent,height=50,
                                   width=50,background='black',borderwidth=0, highlightthickness=0)
            self.frame1.grid(column=0,row=0,sticky=tk.NSEW)
    
            #Frame 1 grid weight
            self.frame1.grid_columnconfigure(0,weight=1)
            self.frame1.grid_rowconfigure(0,weight=1)
            self.frame1.grid_rowconfigure(1,weight=1)
    
            #Some button global properties
            self.buttonWidth = 50
            self.fontList1 = ['Calibri', '12']
    
            #Dummy image, 1x1 pixel
            self.dummyImgB1 = tk.PhotoImage(width=1,height=1)
    
            #Button 1
            self.b1 = tk.Button(self.frame1, borderwidth=0, highlightthickness=0,
                                text='Button height changes', font=self.fontList1, 
                                background='orange', width=self.buttonWidth,
                                height=40, image=self.dummyImgB1, compound=tk.NONE,
                                command=self.unwrapMenu)
            self.b1.grid(column=0,row=0,sticky=tk.NSEW)
    
            #Button 2
            self.b2 = tk.Button(self.frame1, borderwidth=0, highlightthickness=0,
                            text='No text', font=self.fontList1, 
                            background='lime', width=self.buttonWidth,
                            height=60, image=self.dummyImgB1, compound=tk.NONE)
            self.b2.grid(column=0,row=1,sticky=tk.NSEW)      
     
        def setParentGeometry(self) -> None:
            self.parent.geometry('200x100+200+200')
            self.parent.maxsize(200,100)
            self.parent.minsize(200,100)
            self.parent.update()
    
        def unwrapMenu(self) -> None:
    
            #Switch command, set nonzero compound value to show text
            self.b1.configure(command=self.wrapMenu,
                              compound=tk.LEFT)
    
            #Get size of button 1 and print it 
            x = self.b1.winfo_width()
            y = self.b1.winfo_height()
            s = 'Button 1 (wrapped) size: x=' + repr(x) + ', y=' + repr(y)
            print(s)
    
            #Get size of button 2 and print it 
            x = self.b2.winfo_width()
            y = self.b2.winfo_height()
            s = 'Button 2 (wrapped) size: x=' + repr(x) + ', y=' + repr(y)
            print(s)
        
        def wrapMenu(self) -> None:
    
            #Switch command, set compound value to none to hide text
            self.b1.configure(command=self.unwrapMenu,
                              compound=tk.NONE)
    
            #Get size of button 1 and print it 
            x = self.b1.winfo_width()
            y = self.b1.winfo_height()
            s = 'Button 1 (unwrapped) size: x=' + repr(x) + ', y=' + repr(y)
            print(s)
    
            #Get size of button 2 and print it 
            x = self.b2.winfo_width()
            y = self.b2.winfo_height()
            s = 'Button 2 (unwrapped) size: x=' + repr(x) + ', y=' + repr(y)
            print(s)       
    
    # create main window
    window = tk.Tk()
    app = psWindow(window)
    window.mainloop()
    

    200x100应用程序窗口,有两个按钮,第一个按钮的高度为40像素,第二个按钮的高为60像素。两者都有一个图像(1x1像素大小)和文本(默认情况下隐藏)。单击橙色按钮时,将显示其文本并更改其高度。

    Button text hidden Button text showed

    打印输出:

    Button 1 (wrapped) size: x=200, y=40
    Button 2 (wrapped) size: x=200, y=60
    Button 1 (unwrapped) size: x=200, y=44
    Button 2 (unwrapped) size: x=200, y=56
    

    因此,通过将复合选项设置为tk.LEFT来启用文本后,按钮高度将在第二个选项的帮助下增加4个像素。

    我做错了什么?感谢您的宝贵意见!

    0 回复  |  直到 2 年前