0

我有三个问题!

问题 1:我在面板内创建一个标签,它的大小可以更改。我想保持它固定。这是可能的?

如果将光标移动到标签的上限和下限,您将看到光标变为屏幕调整格式。

问题 2:我的按钮占据了面板的整个尺寸。如何调整它的大小而不在下面创建一个空标签?

问题 3:刻度也水平占据了整个面板。可以改变它的大小吗?

from tkinter import*
import tkinter

root = Tk()
root.geometry('900x500')


var_a = DoubleVar()
var_b = DoubleVar()
    
############# CREATING PANELS #####################
#----------- General Panel  --------------#
panel_1 = PanedWindow(bd=4,orient = HORIZONTAL ,relief="raised")#, bg = "red")
panel_1.pack(fill=BOTH, expand=1)    
#----------- Fist Panel  --------------#
panel_3 = PanedWindow(panel_1, orient = VERTICAL, relief="raised")#, bg = "yellow")
panel_1.add(panel_3, minsize=200) #inserting on panel_1
#----------- Second Panel  --------------#
panel_2 = PanedWindow(panel_1, orient = VERTICAL, relief="raised")#, bg = "blue")
panel_1.add(panel_2, minsize=800) #inserting on panel_1
    

label2=Label(panel_3,text="Pass the cursor below me")
panel_3.add(label2)

textbox2=Scale(panel_3,orient=HORIZONTAL,variable = var_a)
panel_3.add(textbox2)

label4=Label(panel_3,text="Pass the cursor above me too")
panel_3.add(label4)

textbox4=Scale(panel_3,orient=HORIZONTAL,variable = var_b)
panel_3.add(textbox4)

def bla():
    pass
button1 = Button(panel_3,text="Why I have this size?", height = 1, width = 1, command= bla())
panel_3.add(button1)


tkinter.mainloop()
4

1 回答 1

1

根据您的问题,很明显,当您在 中添加pack()方法和expand参数时panel,所有小部件容器或面板都根据其父容器展开,每个小部件都适合容器。

从我能够从您的问题中推断出,您可以做的是通过引用它仅适合按钮的水平方向并通过添加参数来使用面板中的小部件扩展来添加pack()带有参数的方法。fill=Xexpand=1

这是我所做的:

from tkinter import*
import tkinter

root = Tk()
root.geometry('900x500')


var_a = DoubleVar()
var_b = DoubleVar()
    
############# CREATING PANELS #####################
#----------- General Panel  --------------#
panel_1 = PanedWindow(bd=4,orient = HORIZONTAL ,relief="raised")#, bg = "red")
panel_1.pack(fill=BOTH, expand=1)    
#----------- Fist Panel  --------------#
panel_3 = PanedWindow(panel_1, orient = VERTICAL, relief="raised")#, bg = "yellow")
panel_1.add(panel_3, minsize=200) #inserting on panel_1
#----------- Second Panel  --------------#
panel_2 = PanedWindow(panel_1, orient = VERTICAL, relief="raised")#, bg = "blue")
panel_1.add(panel_2, minsize=800) #inserting on panel_1
 

label2=Label(panel_3,text="Pass the cursor below me")
panel_3.add(label2)

textbox2=Scale(panel_3,orient=HORIZONTAL,variable = var_a)
panel_3.add(textbox2)

label4=Label(panel_3,text="Pass the cursor above me too")
panel_3.add(label4)

textbox4=Scale(panel_3,orient=HORIZONTAL,variable = var_b)
panel_3.add(textbox4)



def bla():
    pass
button1 = Button(panel_3,text="Why I have this size?", height = 0, width = 0, command= bla())
panel_3.add(button1)
button1.pack(fill=X, expand=1) # Only fits in X (horizontal direction), expands according to the panel




tkinter.mainloop()

请注意,如果要调整比例小部件的大小,可以使用widthheight参数,但这仅适用于几何管理器,例如place(),在这种情况下,您可以使用几何管理器pack()根据面板维护关系和扩展,但是您无法更改它的宽度,因为它是一个几何管理器,它控制和组织小部件,您只能扩展和更改它的比例尺的高度,在这种情况下使用width来更改比例尺小部件的高度。

有关更多信息,请参阅有关几何管理器 pack()的更多信息

于 2021-03-12T17:49:28.907 回答