[英]Resizing in a grid manager tkinter
Using tkinter, I wanted to make an interface containing a few buttons on the left, which would be fairly static and align with the widgets on the right fairly nicely. 使用tkinter,我想创建一个界面,该界面在左侧包含一些按钮,该按钮是相当静态的,并且与右侧的小部件很好地对齐。 On the right, I wanted an Entry widget above a Text widget, both of which would resize accordingly (the Entry widget only on the X axis).
在右侧,我希望在“文本”窗口小部件上方放置一个“入口”窗口小部件,两者都会相应地调整大小(“入口”窗口小部件仅在X轴上)。
This code accomplishes most of that, except the Text widget does not resize and the Entry widget only resizes to align with the Text widget. 该代码完成了大部分工作,除了“文本”小部件不会调整大小,而“输入”小部件仅调整大小以与“文本”小部件对齐。 Upon trying to column/rowconfigure the root, the top frame resizes awkwardly.
尝试对根进行列/行配置时,顶部框架的大小难以调整。
Here's a picture of the tkinter interface from this code: 这是此代码中tkinter接口的图片:
from tkinter import *
def main():
root = Tk()
root.geometry("300x400")
framet = Frame(root)
frameb = Frame(root)
framet.grid(row=0, column=0, sticky='ew')
frameb.grid(row=1, column=0, sticky='news')
button1 = Button(framet, text='one', width=8)
button1.grid(row=0, column=0)
button2 = Button(frameb, text='two', width=8)
button2.grid(row=1, column=0, sticky='n')
entry1 = Entry(framet)
entry1.grid(row=0, column=1, sticky='ew')
text1 = Text(frameb, highlightbackground='black', highlightthickness=1)
text1.grid(row=1, column=1, sticky='news')
framet.columnconfigure(1, weight=1)
if __name__ == '__main__':
main()
As you can see, the Entry and Text widgets do not resize. 如您所见,Entry和Text小部件不会调整大小。 How could I accomplish this whilst still having the Buttons remain static, and not moving anything (only resizing)?
我如何才能做到这一点,同时让Buttons保持静态,并且不移动任何内容(仅调整大小)?
Would this work for you? 这对您有用吗? I changed the lines with
#
comments, I think, I can't really remember what I did I just tryed to get it working, one problem which I'm not happy with though is the entry widget is not the same height as the button, I guess you could manually set its height but.. 我用
#
注释更改了行,我想,我真的不记得我刚刚尝试使它起作用的原因,我不满意的一个问题是输入小部件的高度与按钮的高度不同,我想您可以手动设置其高度,但是..
from tkinter import *
def main():
root = Tk()
root.grid_columnconfigure(1,weight=1) # the text and entry frames column
root.grid_rowconfigure(0,weight=1) # all frames row
buttonframe = Frame(root)
buttonframe.grid(row=0, column=0, sticky="nswe")
entry_text_frame = Frame(root)
entry_text_frame.grid(row=0, column=1, sticky="nswe")
entry_text_frame.grid_columnconfigure(0,weight=1) # the entry and text widgets column
entry_text_frame.grid_rowconfigure(1,weight=1) # the text widgets row
button1 = Button(buttonframe, text='one', width=8)
button1.grid(row=0, column=0, sticky='nswe')
button2 = Button(buttonframe, text='two', width=8)
button2.grid(row=1, column=0, sticky='nswe')
entry1 = Entry(entry_text_frame)
entry1.grid(row=0, column=0, sticky='nswe')
text1 = Text(entry_text_frame, highlightbackground='black', highlightthickness=1)
text1.grid(row=1, column=0, sticky='news')
root.geometry("300x400")
if __name__ == '__main__':
main()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.