根据小部件大小阻止 Tkinter 框架调整大小

编程入门 行业动态 更新时间:2024-10-28 21:21:11
本文介绍了根据小部件大小阻止 Tkinter 框架调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..
root = Tk()

descriptionFrame = Frame(root)

definitionFrame = LabelFrame(descriptionFrame, text="Definition")
definitionScroll = Scrollbar(definitionFrame)
definitionCanvas = Canvas(definitionFrame, width=30, height=4, yscrollcommand=definitionScroll.set)
definitionScroll.config(command=definitionCanvas.yview)
definitionLabel = Label(definitionCanvas, text="n/a")

descriptionFrame.pack()
definitionFrame.pack()
definitionScroll.pack(side=RIGHT, fill=Y)
definitionCanvas.pack(side=LEFT, fill=BOTH, expand=True)
definitionLabel.pack(fill=BOTH, expand=True)

root.mainloop()

我有这个代码.Canvas 设置为宽度为 30,高度为 4,但是当我运行它时,它忽略了 Canvas 的宽度和高度,结果窗口的大小改为围绕 Label 大小.我已经尝试在代码中的每个帧上使用 pack_propagate(False),但它不会影响 definitionFrame 的任何内容,但是当我在 descriptionFrame 它导致一个空窗口.我将如何创建一个 GUI,其中所有框架和窗口的大小都设置为宽 30 和高 4 的 Canvas 大小?

I have this code. The Canvas is set to have a width of 30 and height of 4, but when I run this, it ignores the width and height of the Canvas and the resulting window is sized around the Label instead. I've tried using pack_propagate(False) on every single Frame in the code, but it doesn't affect anything for the definitionFrame, but when I use it on descriptionFrame it results in an empty window. How would I create a GUI where all the frames and the window are sized to the Canvas size of width 30 and height 4?

谢谢.

推荐答案

默认只有 Listbox、Text、Canvas 和 Entry 是可滚动的;Canvas 可以工作,但 IMO 有点矫枉过正,所以这里的东西看起来像是你想要使用 Text

only Listbox, Text, Canvas, and Entry are scrollable by default; Canvas could work, but is a bit overkill IMO, so here's something that seems like what you want using Text

#!/usr/bin/python
from Tkinter import *
root = Tk()

descriptionFrame = Frame(root)

definitionFrame = LabelFrame(descriptionFrame, text="Definition")
definitionScroll = Scrollbar(definitionFrame)
definitionText = Text(definitionFrame, width=30, height=4, yscrollcommand=definitionScroll.set)
definitionScroll.config(command=definitionText.yview)

definitionText.delete("1.0", END)   # an example of how to delete all current text
definitionText.insert("1.0", "n/a") # an example of how to add new text to the text area

descriptionFrame.pack(fill=BOTH, expand=True)
definitionFrame.pack(fill=BOTH, expand=True)
definitionScroll.pack(side=RIGHT, fill=Y)
definitionText.pack(side=LEFT, fill=BOTH, expand=True)

root.mainloop()

这篇关于根据小部件大小阻止 Tkinter 框架调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

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

发布评论

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

>www.elefans.com

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