问题描述
限时送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:关键词]
发布评论