【问题标题】:How do I set the Text in tkinter size to the size of the main window?如何将 tkinter 大小的文本设置为主窗口的大小?
【发布时间】:2021-01-04 07:08:32
【问题描述】:

我在 tkinter 中创建了一个文本区域,我希望每次调整窗口大小时它的大小都适合 Tk()!
这是我的代码:

from tkinter import *

#The window:
app = Tk()

#The size of the screen:
s_width = app.winfo_screenwidth()
s_height = app.winfo_screenheight()

#The text area:
text_area = Text(app, width = s_width, height = s_height)
text_area.place(x = 0, y = 0)

#mainloop
app.mainloop()

但这给 text_area 带来了奇怪的大小!
我希望 text_area 的大小与 app 变量一样大!
我该怎么做?
每次我调整屏幕大小时它都应该调整大小!

【问题讨论】:

    标签: python tkinter text resize


    【解决方案1】:

    当您使用屏幕分辨率作为文本框的宽度和高度时,您创建了一个非常大的Text 框。请注意,Text 小部件的 widthheight 选项是字符,而不是像素。因此,对于 1920x1080 的屏幕分辨率,您创建了一个具有 1920 个字符宽度和 1080 行高度的 Text 框。

    要使文本框随根窗口一起调整大小,可以使用place()relwidthrelheight 选项:

    from tkinter import *
    
    #The window:
    app = Tk()
    
    #The text area:
    text_area = Text(app)
    text_area.place(x=0, y=0, relwidth=1, relheight=1)
    
    #mainloop
    app.mainloop()
    

    【讨论】:

      【解决方案2】:

      text_area.place(x = 0, y = 0)

      • 我想试着把你的价值 尝试参考文档 *

      • 可以创建文档here

      【讨论】:

      • 我想,嗯...这不是正确的答案!或者可能是?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-31
      • 2013-09-09
      • 2019-02-25
      • 2022-06-18
      • 2022-01-13
      • 1970-01-01
      相关资源
      最近更新 更多