Python tkinter 文本小部件使​​用网格填充固定大小的框架

编程入门 行业动态 更新时间:2024-10-23 12:35:20
本文介绍了Python tkinter 文本小部件使​​用网格填充固定大小的框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

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

Running Python 3.4.2 and Tkinter 8.6 on a Raspberry Pi. I want to create a Text widget that fills a fixed sized frame using the grid layout manager.

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

If you run the code below, you'll see the Text widget doesn't fill the 800x600 frame. I realize I could set the Text's width and height attributes to fill the frame, but this would only work default font and wouldn't fill the frame exactly.

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()

推荐答案

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

You need to configure the grid to give the row and column a nonzero weight so that they expand

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()

这篇关于Python tkinter 文本小部件使​​用网格填充固定大小的框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 05:49:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1390186.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:网格   部件   框架   文本   大小

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!