【问题标题】:Python tkinter Text widget fill fixed sized frame using gridPython tkinter Text 小部件使用网格填充固定大小的框架
【发布时间】:2016-07-22 13:55:52
【问题描述】:

在 Raspberry Pi 上运行 Python 3.4.2 和 Tkinter 8.6。我想创建一个使用网格布局管理器填充固定大小框架的文本小部件。

如果您运行下面的代码,您会看到文本小部件没有填充 800x600 帧。我意识到我可以设置文本的宽度和高度属性来填充框架,但这只能使用默认字体并且不会完全填充框架。

from tkinter import *

class Test(Tk):
  def __init__(self):
    super().__init__()
    self.text = frameText(self)
    self.geometry('{}x{}'.format(800, 600))   

def frameText(frame, **kw):
  ysb = Scrollbar(frame)
  xsb = Scrollbar(frame, orient = HORIZONTAL)
  text = Text(frame, **kw)
  ysb.configure(command = text.yview)
  xsb.configure(command = text.xview)
  text.configure(yscrollcommand = ysb.set, xscrollcommand = xsb.set)
  text.grid(row = 0, column = 0, sticky = N+E+S+W)
  ysb.grid(row = 0, column = 1, sticky = N+S)
  xsb.grid(row = 1, column = 0, sticky = E+W)
  return text

if __name__ == '__main__':
  Test().mainloop()

【问题讨论】:

    标签: python tkinter


    【解决方案1】:

    您需要配置网格以赋予行和列非零权重,以便它们展开

    from tkinter import *
    
    class Test(Tk):
      def __init__(self):
        Tk.__init__(self)
        self.grid_columnconfigure(0, weight=1)
        self.grid_rowconfigure(0, weight=1)
        self.text = frameText(self)
        self.geometry('{}x{}'.format(800, 600))   
    
    def frameText(frame, **kw):
      ysb = Scrollbar(frame)
      xsb = Scrollbar(frame, orient = HORIZONTAL)
      text = Text(frame, **kw)
      ysb.configure(command = text.yview)
      xsb.configure(command = text.xview)
      text.configure(yscrollcommand = ysb.set, xscrollcommand = xsb.set)
      text.grid(row = 0, column = 0, sticky = N+E+S+W)
      ysb.grid(row = 0, column = 1, sticky = "ns")
      xsb.grid(row = 1, column = 0, sticky = E+W)
      return text
    
    if __name__ == '__main__':
      Test().mainloop()
    

    【讨论】:

      猜你喜欢
      • 2015-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-04
      • 2014-08-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多