Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Tkinter按钮位于同一行中,但有两列滚动文本_Python_Python 3.x_Tkinter - Fatal编程技术网

Python Tkinter按钮位于同一行中,但有两列滚动文本

Python Tkinter按钮位于同一行中,但有两列滚动文本,python,python-3.x,tkinter,Python,Python 3.x,Tkinter,我正在使用python3的tkinter在GUI上运行。 对于一帧,我希望结果如下: 但当我尝试使用此代码时: master.title("Homepage") master.title("Window to check information") master.geometry('%dx%d+%d+%d' % (850, 800, (master.winfo_screenwidth() - 850) / 2, 0)) self.information = tkst.ScrolledText(

我正在使用python3的tkinter在GUI上运行。 对于一帧,我希望结果如下: 但当我尝试使用此代码时:

master.title("Homepage")
master.title("Window to check information")
master.geometry('%dx%d+%d+%d' % (850, 800, (master.winfo_screenwidth() - 850) / 2, 0))

self.information = tkst.ScrolledText(self, wrap=tk.WORD, height=20, width=100)
self.btn1 = tk.Button(self, text='Cours', height=3, width=40)
self.btn2 = tk.Button(self, text='Absences',  height=3, width=40)
self.btn3 = tk.Button(self, text='Notes',  height=3, width=40)
self.btn4 = tk.Button(self, text='Return',  height=3, width=40)

self.information.config(font=font.Font(size=15))
self.information.configure(background='#C0C0C0')

self.btn2.config(font=font.Font(size=12))
self.btn3.config(font=font.Font(size=12))
self.btn1.config(font=font.Font(size=12))
self.btn4.config(font=font.Font(size=12))

self.information.grid(row=0,column=0)
self.btn1.grid(row=1,column=0)
self.btn2.grid(row=1,column=1)
self.btn3.grid(row=2,column=0)
self.btn4.grid(row=2,column=1)
我发现GUI是这样的:


有人能帮助我如何编写网格或包的代码来实现第一张图片吗?谢谢。

要使
滚动文本跨越两列,您需要使用网格中的
列span
选项:

self.information.grid(row=0, column=0, columnspan=2)

同样的效果也可以通过
pack
方法实现,我发现这种方法更可取,因为它比网格系统更好地处理窗口扩展

有关如何实现这一点的模型,请参见下文:

from tkinter import *

root = Tk()

frametop = Frame(root)
framebottom = Frame(root)
frameleft = Frame(framebottom)
frameright = Frame(framebottom)

text = Text(frametop)
scroll = Scrollbar(frametop, command=text.yview)
btn1 = Button(frameleft, text="Course")
btn2 = Button(frameleft, text="Abscences")
btn3 = Button(frameright, text="Notes")
btn4 = Button(frameright, text="Return")

text['yscrollcommand'] = scroll.set

frametop.pack(side=TOP, fill=BOTH, expand=1)
framebottom.pack(side=BOTTOM, fill=BOTH, expand=1)
frameleft.pack(side=LEFT, fill=BOTH, expand=1)
frameright.pack(side=RIGHT, fill=BOTH, expand=1)

text.pack(side=TOP, fill=BOTH, padx=5, pady=5, expand=1)
scroll.pack(side=BOTTOM, fill=BOTH, padx=5, pady=5, expand=1)
btn1.pack(side=TOP, fill=BOTH, padx=5, pady=5, expand=1)
btn2.pack(side=BOTTOM, fill=BOTH, padx=5, pady=5, expand=1)
btn3.pack(side=TOP, fill=BOTH, padx=5, pady=5, expand=1)
btn4.pack(side=BOTTOM, fill=BOTH, padx=5, pady=5, expand=1)

root.mainloop()
这将创建四个框架,并将项目打包到其指定位置,并在它们之间添加填充:

扩大或缩小窗口(在合理范围内,缩小太多会导致Tkinter开始隐藏项目)将导致项目自动移动并更改大小