Tkinter - 添加滚动条时文本小部件会缩小

时间:2013-05-29 05:33:26

标签: python python-2.7 tkinter

我正在使用Python 2.7中的Tkinter进行简单的测验应用程序的GUI。 到目前为止,我已经开始设置我的框架。我在一个名为results_txtbx的文本小部件中放置一个滚动条,向上和向下滚动一个列表,注明玩家在每个问题上的表现。我一直在使用grid,因为我更容易管理。

from Tkinter import *

class Q_and_A:

    def __init__(self, master):
        frame = Frame(master)

        Label(master).grid(row = 4)

        results_txtbx = Text(master)
        results_scrbr = Scrollbar(results_txtbx)
        results_scrbr.grid(sticky = NS + E)

        results_txtbx.config(width = 20, height = 4, wrap = NONE, yscrollcommand = results_scrbr.set)
        results_txtbx.grid(row = 3, column = 1, padx = 12, sticky = W)



root = Tk()
root.wm_title("Question and Answer")
root.resizable(0, 0)

app = Q_and_A(root)
root.mainloop()

当运行时,results_txtbx会调整大小以适应滚动条。有没有办法让它使用grid保持原始大小?

3 个答案:

答案 0 :(得分:3)

您不希望将文本小部件用作滚动条的主控。与任何其他窗口小部件一样,如果您在文本窗口小部件中打包或网格化滚动条,则文本窗口小部件将缩小或展开以适合滚动条。这是你问题的症结所在。

相反,创建一个单独的框架(您已经在做),并使用该框架作为文本窗口小部件和滚动条的父框架。如果您想要滚动条位于其中的外观,请将文本小部件的边框宽度设置为零,然后为包含的框架指定一个小边框。

作为最终的可用性提示,我建议使窗口不可调整大小。您的用户可能比您更了解他们想要的窗口大小。不要将控制权从用户手中夺走。

这是(大致)我将如何实现您的代码:

  • 我会使用import Tkinter as tk而不是from Tkinter import *,因为全球导入通常是一个坏主意。
  • 我会将Q_and_A作为tk.Frame的子类,以便将其视为小部件。
  • 我会让整个窗口可调整大小
  • 我会将小部件创建与小部件布局分开,因此我的所有布局选项都在一个地方。这使IMO更容易编写和维护。
  • 如我的回答所述,我会将文本和滚动条小部件放在框架中

这是最终结果:

import Tkinter as tk

class Q_and_A(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master, borderwidth=1, relief="sunken")
        self.label = tk.Label(self)
        self.results_txtbx = tk.Text(self, width=20, height=4, wrap="none",
                                     borderwidth=0, highlightthickness=0)
        self.results_scrbr = tk.Scrollbar(self, orient="vertical", 
                                          command=self.results_txtbx.yview)
        self.results_txtbx.configure(yscrollcommand=self.results_scrbr.set)

        self.label.grid(row=1, columnspan=2)
        self.results_scrbr.grid(row=0, column=1, sticky="ns")
        self.results_txtbx.grid(row=0, column=0, sticky="nsew")
        self.grid_rowconfigure(0, weight=1)
        self.grid_columnconfigure(0, weight=1)

root = tk.Tk()
root.wm_title("Question And Answer")

app = Q_and_A(root)
app.pack(side="top", fill="both", expand=True)
root.mainloop()

答案 1 :(得分:1)

results_scrbr.grid(row = 3, column = 2)旁边设置results_txtbx.grid(row = 3,column = 1, padx = 4),不需要sticky,因为窗口不可调整大小,并且我降低了padx,因此滚动条更靠近文本。

另外,为了使results_txtbx可以垂直滚动,请添加results_scrbr.config(command=results_txtbx.yview)

这是一个有效的代码......

from Tkinter import *

class Q_and_A:

    def __init__(self, master):
        frame = Frame(master)

        Label(master).grid(row = 4)

        results_txtbx = Text(master)
        results_scrbr = Scrollbar(master)
        results_scrbr.grid(row = 3, column = 2)
        results_scrbr.config(command=results_txtbx.yview)


        results_txtbx.config(width = 20, height = 4,
                             wrap = NONE, yscrollcommand = results_scrbr.set)
        results_txtbx.grid(row = 3, column = 1, padx = 4)



root = Tk()
root.wm_title("Question and Answer")
root.resizable(0, 0)

app = Q_and_A(root)
root.mainloop()

答案 2 :(得分:0)

我实施的解决方案:

我需要向应用添加更多小部件,因此我将ScrollbarText小部件绑定到另一个标签,并将其放在正确的列中,代码(为了便于阅读而修剪)如下: / p>

import Tkinter as tk

class Q_and_A(tk.Frame):

    def __init__(self, master):
        tk.Frame.__init__(self, master)

        self.label = tk.Label(self)

        #Set up menu strip

        self.main_menu = tk.Menu(self)
        self.file_menu = tk.Menu(self.main_menu, tearoff = 0)

        self.file_menu.add_command(label = "Exit", command = self.quit)

        self.main_menu.add_cascade(label = "File", menu = self.file_menu)

        self.master.config(menu = self.main_menu)

        #Set up labels

        self.question_lbl = tk.Label(self, text = "Question #: ", padx = 12, pady = 6)
        self.question_lbl.grid(row = 0, sticky = "w")

        tk.Label(self, text = "Hint: ").grid(row = 1, sticky = "w", padx = 12, pady = 6)
        tk.Label(self, text = "Answer: ").grid(row = 2, sticky = "w", padx = 12, pady = 6)
        tk.Label(self, text = "Results: ").grid(row = 3, sticky = "nw", padx = 12, pady = 6)

        tk.Label(self).grid(row = 4)

        #Set up textboxes
        self.question_txtbx = tk.Entry(self)
        self.question_txtbx.config(width = 60)
        self.question_txtbx.grid(row = 0, column = 1, padx = 12, columnspan = 3, sticky = "w")
        self.help_txtbx = tk.Entry(self)
        self.help_txtbx.config(width = 40)
        self.help_txtbx.grid(row = 1, column = 1, columnspan = 2, padx = 12, sticky = "w")
        self.answer_txtbx = tk.Entry(self)
        self.answer_txtbx.config(width = 40)
        self.answer_txtbx.grid(row = 2, column = 1, columnspan = 2, padx = 12, sticky = "w")

        self.results_label = tk.Label(self)

        self.results_txtbx = tk.Text(self.results_label, width = 10, height = 4, wrap = "none", borderwidth = 1, highlightthickness = 1)
        self.results_scrbr = tk.Scrollbar(self.results_label, orient = "vertical", command = self.results_txtbx.yview)
        self.results_txtbx.configure(yscrollcommand = self.results_scrbr.set)

        self.label.grid(row = 1)
        self.results_label.grid(row = 3, column = 1, padx = 11, sticky = "w")
        self.results_scrbr.grid(row = 0, column = 1, sticky = "nse")
        self.results_txtbx.grid(row = 0, column = 0, sticky = "w")

root = tk.Tk()
root.wm_title("Question and Answer")
#A note: The window is non-resizable due to project specifications.
root.resizable(0, 0)

app = Q_and_A(root)
app.pack(side = "top", fill = "both")
root.mainloop()

我将存储在嵌套标签中作为我自己的参考,因为当我需要将事物放在一起时,除非有某些原因应该避免。在这里工作得很好。感谢Bryan的建议。