2017-08-09 107 views
-1

我正在使用python3的tkinter在GUI上進行操作。 對於一個框架,我想結果是這樣的: enter image description here 但是當我使用此代碼嘗試:Tkinter按鈕在同一行,但有一個滾動文字2列

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是這樣的: enter image description here

有人能幫助我如何能我寫網格或包的代碼來實現第一張照片?謝謝。

回答

4

爲了使ScrolledText跨度兩列,您需要使用columnspan選項電網:

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

非常感謝。這就是我想要的XD – Minalinskey

0

同樣的效果,也可以用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() 

這將創建四個幀和包裝的物品到他們指定的地方與他們之間的填充:

enter image description here

展開或縮小窗口(在合理範圍內,收縮太多會導致Tkinter開始隱藏項目)將導致項目自動移動並更改大小。

相關問題