Python Tkinter滚动条没有';t填充框

Python Tkinter滚动条没有';t填充框,python,tkinter,scrollbar,Python,Tkinter,Scrollbar,我正在尝试制作一个GUI,它根据用户的分辨率进行更改。我对Python还是相当陌生的,但我已经在某种程度上实现了这一点。我正在努力寻找为什么滚动条没有使用给它们的所有空间。这导致它们后面的框架可见 root = Tk() screen_height = root.winfo_screenheight() screen_width = root.winfo_screenwidth() h = (screen_height / 800) w = (screen_width / 1300) fr

我正在尝试制作一个GUI,它根据用户的分辨率进行更改。我对Python还是相当陌生的,但我已经在某种程度上实现了这一点。我正在努力寻找为什么滚动条没有使用给它们的所有空间。这导致它们后面的框架可见

root = Tk()

screen_height = root.winfo_screenheight()
screen_width = root.winfo_screenwidth()

h = (screen_height / 800)
w = (screen_width / 1300)

frame = ['topleft', 'topmid', 'topright',
     'bottomleft', 'bottommid']
multiplier = [300, 200, 800, 500, 100]
height = [0,
      Decimal(h*(multiplier[0]))
      .quantize(Decimal('1.'), rounding=ROUND_UP),
      Decimal(h*(multiplier[1]))
      .quantize(Decimal('1.'), rounding=ROUND_UP),
      Decimal(h*(multiplier[2]))
      .quantize(Decimal('1.'), rounding=ROUND_UP),
      Decimal(h*(multiplier[3]))
      .quantize(Decimal('1.'), rounding=ROUND_UP),
      Decimal(h*(multiplier[4]))
      .quantize(Decimal('1.'), rounding=ROUND_UP)
     ]
width = [0,
     Decimal(w*(multiplier[0]))
     .quantize(Decimal('1.'), rounding=ROUND_UP),
     Decimal(w*(multiplier[1]))
     .quantize(Decimal('1.'), rounding=ROUND_UP),
     Decimal(w*(multiplier[2]))
     .quantize(Decimal('1.'), rounding=ROUND_UP),
     Decimal(w*(multiplier[3]))
     .quantize(Decimal('1.'), rounding=ROUND_UP),
     Decimal(w*(multiplier[4]))
     .quantize(Decimal('1.'), rounding=ROUND_UP),
     Decimal(w*(multiplier[4]))
     .quantize(Decimal('1.'), rounding=ROUND_UP),
    ]

topleft = Frame(root,
            bg = 'black', bd='3',
            height = height[1], width = width[1],
            relief='flat'
            ).grid(row = height[0], column = width[0],
                   rowspan = height[1], columnspan = width[1])
topmid = Frame(root,
           bg = 'yellow', bd='3',
           height = height[2], width = width[2],
           relief='flat'
           ).grid(row = height[0], column = width[1],
                  rowspan = height[2], columnspan = width[2])
bottommid = Frame(root,
              bg = 'pink', bd ='3',
              height = height[5], width = width[5],
              relief='flat'
              ).grid(row = height[2], column = width[1],
                     rowspan = height[5], columnspan = width[5])
bottomright = Frame(root,
                bg = 'blue', bd='3',
                height = height[5], width = width[5],
                relief='flat'
                ).grid(row = height[2], column = width[1]+width[5],
                       rowspan = height[5], columnspan = width[5])

class file_selector:
def __init__(self):
    global fs_scrollbar, fs_listbox, check
    bottomleft = Frame(root,
               bg = 'white', bd='3',
               height = height[4], width = width[4],
               relief='flat'
               ).grid(row= height[1], column= width[0],
                      rowspan= height[4], columnspan= width[4])

    self.fs_scrollbar = Scrollbar(bottomleft,
                          orient = VERTICAL
                          )

    self.fs_listbox = Listbox(bottomleft,
                      bg = 'white',
                      relief = 'flat',
                      yscrollcommand = self.fs_scrollbar.set
                      )

    self.check = print(self.fs_listbox.curselection())

    self.fs_listbox.bind('<ButtonRelease-1>', self.check)

    self.fs_scrollbar['command'] = self.fs_listbox.yview

    file_selector.widgets(self)

def widgets(self):        

    self.fs_scrollbar.grid(row = height[1], column = (width[4]-(width[4]*Decimal(0.05)).quantize(Decimal('1.'), rounding=ROUND_UP)),
                           rowspan = height[4], columnspan = ((width[4]*Decimal(0.05)).quantize(Decimal('1.'), rounding=ROUND_UP)),
                           sticky= N+E+S+W
                           )

    for file in os.listdir(os.curdir):
        if file.endswith(".txt"):
            self.fs_listbox.insert(END, file)

    self.fs_listbox.grid(row = height[1], column = width[0],
                         rowspan = height[4], columnspan = (width[4]-(width[4]*Decimal(0.05)).quantize(Decimal('1.'), rounding=ROUND_UP)),
                         sticky = N+E+S+W
                         )



class text_editor:
def __init__(self):
    global te_scrollbar, te_text
    topright = Frame(root,
             bg = 'red', bd='3',
             height = height[3], width = width[3],
             relief='flat'
             ).grid(row=height[0], column= width[4],
                    rowspan= height[3], columnspan= width[3])

    self.te_scrollbar = Scrollbar(topright,
                                  orient = VERTICAL
                                  )

    self.te_text = Text(topright,
                bg = 'white',
                yscrollcommand = self.te_scrollbar.set
                )

    self.te_scrollbar['command'] = self.te_text.yview

    text_editor.widgets(self)

def widgets(self):
    with open('test1.txt', 'r') as self.file:
         self.file = self.file.read()

    self.te_scrollbar.grid(row = height[0], column = (width[4]+width[3]-(width[3]*Decimal(0.05)).quantize(Decimal('1.'), rounding=ROUND_UP)),
                           rowspan = height[3], columnspan = ((width[3]*Decimal(0.05)).quantize(Decimal('1.'), rounding=ROUND_UP)),
                           sticky = N+S
                           )

    self.te_text.insert(END, self.file)

    self.te_text.grid(row = height[0], column = width[4],
                      rowspan = height[3], columnspan = (width[3]-(width[3]*Decimal(0.05)).quantize(Decimal('1.'), rounding=ROUND_UP)),
                      sticky = N+E+S+W
                      )

file_selector()
text_editor()
root=Tk()
screen\u height=root.winfo\u screenheight()
screen\u width=root.winfo\u screenwidth()
h=(屏幕高度/800)
w=(屏幕宽度/1300)
frame=['topleft'、'topmid'、'topright',
'bottomleft'、'bottommid']
乘数=[300200800500100]
高度=[0,
十进制(h*(乘数[0]))
.量化(十进制('1'),四舍五入=四舍五入),
十进制(h*(乘数[1]))
.量化(十进制('1'),四舍五入=四舍五入),
十进制(h*(乘数[2]))
.量化(十进制('1'),四舍五入=四舍五入),
十进制(h*(乘数[3]))
.量化(十进制('1'),四舍五入=四舍五入),
十进制(h*(乘数[4]))
.量化(十进制('1'),四舍五入=四舍五入)
]
宽度=[0,
十进制(w*(乘数[0]))
.量化(十进制('1'),四舍五入=四舍五入),
十进制(w*(乘数[1]))
.量化(十进制('1'),四舍五入=四舍五入),
十进制(w*(乘数[2]))
.量化(十进制('1'),四舍五入=四舍五入),
十进制(w*(乘数[3]))
.量化(十进制('1'),四舍五入=四舍五入),
十进制(w*(乘数[4]))
.量化(十进制('1'),四舍五入=四舍五入),
十进制(w*(乘数[4]))
.量化(十进制('1'),四舍五入=四舍五入),
]
左上=帧(根,
bg=‘黑色’,bd=‘3’,
高度=高度[1],宽度=宽度[1],
宽缓‘
).grid(行=高度[0],列=宽度[0],
行跨度=高度[1],列跨度=宽度[1])
topmid=框架(根、,
bg=‘黄色’,bd=‘3’,
高度=高度[2],宽度=宽度[2],
宽缓‘
).grid(行=高度[0],列=宽度[1],
行跨度=高度[2],列跨度=宽度[2])
底部中间=框架(根,
bg=‘粉红’,bd=‘3’,
高度=高度[5],宽度=宽度[5],
宽缓‘
).网格(行=高度[2],列=宽度[1],
行跨度=高度[5],列跨度=宽度[5])
bottomright=帧(根,
bg=‘蓝色’,bd=‘3’,
高度=高度[5],宽度=宽度[5],
宽缓‘
).网格(行=高度[2],列=宽度[1]+宽度[5],
行跨度=高度[5],列跨度=宽度[5])
类文件选择器:
定义初始化(自):
全局fs_滚动条,fs_列表框,选中
左下=帧(根,
bg=‘白色’,bd=‘3’,
高度=高度[4],宽度=宽度[4],
宽缓‘
).grid(行=高度[1],列=宽度[0],
行跨度=高度[4],列跨度=宽度[4])
self.fs_scrollbar=滚动条(左下角,
方向=垂直
)
self.fs_listbox=listbox(左下角,
bg=‘白色’,
卸压=‘平’,
yscrollcommand=self.fs\u scrollbar.set
)
self.check=print(self.fs\u listbox.curselection())
self.fs_listbox.bind(“”,self.check)
self.fs_滚动条['command']=self.fs_listbox.yview
文件选择器.widgets(self)
def小部件(自):
self.fs_scrollbar.grid(行=高度[1],列=(宽度[4]-(宽度[4]*十进制(0.05)).quantize(十进制('1')),舍入=向上舍入)),
rowspan=高度[4],columnspan=((宽度[4]*小数点(0.05))。量化(小数点('1')),舍入=向上舍入),
粘性=N+E+S+W
)
对于os.listdir(os.curdir)中的文件:
如果文件.endswith(“.txt”):
self.fs\u listbox.insert(结束,文件)
self.fs_listbox.grid(行=高度[1],列=宽度[0],
行span=高度[4],列span=(宽度[4]-(宽度[4]*小数点(0.05))。量化(小数点('1'),四舍五入=四舍五入)),
粘性=N+E+S+W
)
类文本编辑器:
定义初始化(自):
全局te_滚动条,te_文本
右上=帧(根,
bg=‘红色’,bd=‘3’,
高度=高度[3],宽度=宽度[3],
宽缓‘
).grid(行=高度[0],列=宽度[4],
行跨度=高度[3],列跨度=宽度[3])
self.te_scrollbar=滚动条(右上角,
方向=垂直
)
self.te_text=文本(右上角,
bg=‘白色’,
yscrollcommand=self.te_scrollbar.set
)
self.te_滚动条['command']=self.te_text.yview
文本编辑器小部件(自身)
def小部件(自):
将open('test1.txt','r')作为self.file:
self.file=self.file.read()
self.te_scrollbar.grid(行=高度[0],列=(宽度[4]+宽度[3]-(宽度[3]*十进制(0.05)).quantize(十进制('1')),舍入=向上舍入)),
rowspan=高度[3],columnspan=((宽度[3]*小数点(0.05))。量化(小数点('1')),舍入=向上舍入),
粘性=N+S
)
self.te_text.insert(结束,self.file)
self.te_text.grid(行=高度[0],列=宽度[4],
行span=高度[3],列span=(宽度[3]-(宽度[3]*小数点(0.05))。量化(小数点('1'),四舍五入=四舍五入)),
粘性=N+E+S+W
)
文件选择器()
文本编辑器()

事先为糟糕的代码感到抱歉,Tkinter没有很好的文档记录(我也不是很在行)。

您不需要进行所有这些计算——Tkinter非常擅长计算几何体

在使用网格时,您会犯一些非常常见的错误。当容器中有额外空间时(例如:F
topleft = Frame(...).grid(...)