我已經並排創建了兩個文本框小部件。左邊的文本框包含100行Checkbutton框。正確的文本框包含100行文本。我已經將兩個文本框的高度設置爲30,寬度爲10.Tkinter:使用Checkbutton + Textbar內的滾動條小工具
當我運行該程序時,左側的文本框垂直增長超出了我的可視屏幕區域的大小。結果,正確的文本框被推到框架的盡頭,我再也看不到它了。我知道它在那裏,因爲當我將行數(對於兩個框)減少到50(而不是100)時,我可以看到正確的文本框。
此外,我注意到左側的滾動條沒有連接到左側的文本框。但是,右側滾動條正確連接到正確的文本框。
能否請你幫我明白了:
1)爲什麼左文本框垂直增長(定義文本小尺寸外)和不同步右側文本框中的大小和位置?
2)爲什麼左側的滾動條不附加到左側的文本控件(如右側的文本框)?
這是到目前爲止我的代碼:
#!/usr/bin/python
from Tkinter import *
class MyApp:
def __init__(self, parent):
self.myParent = parent
self.main_container = Frame(self.myParent)
self.main_container.grid(row=0, column=0, columnspan=4)
self.left_frame = Frame(self.main_container)
self.left_frame.grid(row=0, column=0, columnspan=2)
self.right_frame = Frame(self.main_container)
self.right_frame.grid(row=0, column=2, columnspan=2)
self.checkbox_scrollbar = Scrollbar(self.left_frame)
self.checkbox_scrollbar.grid(row=0, column=1, sticky='NS')
self.checkbox_text = Text(self.left_frame, height=30, width=10, yscrollcommand=self.checkbox_scrollbar.set)
self.checkbox_text.grid(row=0, column=0)
self.checkbox_scrollbar.config(command=self.checkbox_text.yview)
self.databox_scrollbar = Scrollbar(self.right_frame)
self.databox_scrollbar.grid(row=0, column=1, sticky='NS')
self.databox_text = Text(self.right_frame, height=30, width=10, yscrollcommand=self.databox_scrollbar.set)
self.databox_text.grid(row=0, column=0)
self.databox_scrollbar.config(command=self.databox_text.yview)
my_dict = {}
for each_num in range(100):
my_line = "Line number: " + str(each_num)
my_dict[my_line] = 0
for my_key in my_dict:
my_dict[my_key] = IntVar()
cb = Checkbutton(self.checkbox_text, text=my_key, variable=my_dict[my_key])
cb.grid(sticky='W')
for each_entry in range(100):
entry_line = "This is entry number: " + str(each_entry) + "\n"
self.databox_text.insert(END, entry_line)
root = Tk()
root.title("Checkbox UI Test")
myapp = MyApp(root)
root.mainloop()
謝謝您的建議。經過多次試驗和錯誤之後,我發現解決方案是使用Text小部件的window_create方法來嵌入對象(比如我的checkbutton對象)。 – 2013-02-20 03:53:28
@DirtyPenguin哦整齊,很高興你整理出來。抱歉,我無法幫助你。 – TankorSmash 2013-02-20 03:55:05
不用擔心。您的建議更改背景顏色對調試非常有用。我會記住它向前發展。再次感謝。 – 2013-02-20 04:00:56