如果Python小部件到达屏幕的末尾,则它们会在框架中包装小部件

时间:2013-11-16 00:09:22

标签: python user-interface tkinter tk ttk

对于Tk几何管理器,是否有类似包装到新行的东西?我正在使用pack将小部件放在框架内。在高分辨率屏幕上,小部件适合并排查找。但是,如果将其放入较小的屏幕,则小部件将在框架中用尽。

基本上,我们来自:

Before

为:

after

您可以看到它如何切断我的处理器输入字段。相关代码:

options_frame = ttk.LabelFrame(
        parent_frame, text="Blast Options")
options_frame.pack(side=TOP, fill=X, expand=1, padx=5, pady=5)
        self._set_up_blast_options(options_frame)

def _set_up_blast_options(self, options_frame):
    self.evalue = Tkinter.DoubleVar()
    self.evalue.set(1)
    self.word_size = Tkinter.IntVar()
    self.word_size.set(4)
    self.penalty_mismatch = Tkinter.DoubleVar()
    self.penalty_mismatch.set(-4)
    self.min_d_match = Tkinter.IntVar()
    self.min_d_match.set(5)
    self.proc_count = Tkinter.IntVar()
    self.proc_count.set(cpu_count())

    # evalue
    e_value_label = ttk.LabelFrame(
        options_frame, text="e-Value Threshold")
    e_value_entry = ttk.Entry(e_value_label)
    e_value_entry.insert(0, self.evalue.get())
    e_value_entry.bind('<Return>', self._validate_e_value)
    e_value_entry.bind('<FocusOut>', self._validate_e_value)
    e_value_label.pack(side=LEFT, expand=1, pady=5, padx=5, fill=X)
    e_value_entry.pack(side=TOP, expand=1, pady=5, padx=5, fill=X)

    # word size
    word_size_label = ttk.LabelFrame(
        options_frame, text="Word Size")
    word_size_entry = ttk.Entry(word_size_label)
    word_size_entry.insert(0, self.word_size.get())
    word_size_entry.bind('<Return>', self._validate_word_value)
    word_size_entry.bind('<FocusOut>', self._validate_word_value)
    word_size_label.pack(side=LEFT, expand=1, pady=5, padx=5, fill=X)
    word_size_entry.pack(side=TOP, expand=1, pady=5, padx=5, fill=X)

    penalty_mismatch_label = ttk.LabelFrame(
        options_frame, text="Penalty Mismatch")
    penalty_mismatch_entry = ttk.Entry(penalty_mismatch_label)
    penalty_mismatch_entry.insert(0, self.penalty_mismatch.get())
    penalty_mismatch_entry.bind(
        '<Return>', self._validate_penalty_mismatch_value)
    penalty_mismatch_entry.bind(
        '<FocusOut>', self._validate_penalty_mismatch_value)
    penalty_mismatch_label.pack(side=LEFT, expand=1, pady=5,
                                padx=5, fill=X)
    penalty_mismatch_entry.pack(side=TOP, expand=1, pady=5,
                                padx=5, fill=X)
    # Min D Nucleotides
    min_d_match_label = ttk.LabelFrame(
        options_frame, text="Minimal Number of D Nucleotides")
    min_d_match_entry = ttk.Entry(min_d_match_label)
    min_d_match_entry.insert(0, self.min_d_match.get())
    min_d_match_entry.bind(
        '<Return>', self._validate_min_match_value)
    min_d_match_entry.bind(
        '<FocusOut>', self._validate_min_match_value)
    min_d_match_label.pack(side=LEFT, expand=1, pady=5, padx=5, fill=X)
    min_d_match_entry.pack(side=TOP, expand=1, pady=5, padx=5, fill=X)

    # how many cpus to use
    proc_count_label = ttk.LabelFrame(
        options_frame, text="Processors")
    proc_count_entry = ttk.Entry(proc_count_label)
    proc_count_entry.insert(0, self.proc_count.get())
    proc_count_entry.bind(
        '<Return>', self._validate_proc_count_value)
    proc_count_entry.bind(
        '<FocusOut>', self._validate_proc_count_value)
    proc_count_label.pack(side=LEFT, expand=1, pady=5, padx=5, fill=X)
    proc_count_entry.pack(side=TOP, expand=1, pady=5, padx=5, fill=X)

许多事件绑定和选项的设置可能不相关,但我认为我仍然应该包括......以防万一。最好的选择是切换到网格管理器吗?问题就在于这个框架被打包在另一个框架内......依此类推。如果您开始使用网格管理器,那么您必须将它用于所有内容,因为您无法在同一帧中混合打包和网格管理器吗?

2 个答案:

答案 0 :(得分:2)

AFAIK没有在tkinter中创建动态接口的内置方法。您最好的选择是获得屏幕分辨率,然后适当地进行网格/打包。

post{
      entity(as[Project]) { project =>
          complete {
            (projectActor ? Update(project)).mapTo[Project]
          }
      }
   }

所以举例......

user32 = ctypes.windll.user32
get_screensize = (user32.GetSystemMetrics(0), user32.GetSystemMetrics(1))

答案 1 :(得分:2)

一般来说,我同意I_do_python的答案,但我会做一个小修改。我会将所有小部件放在一个新的框架中,我会使用框架的宽度作为是否包裹的试金石。并且您可以绑定"<Configure>"事件的事件处理程序,然后检查在应用程序的窗口(以及因此框架)调整大小时是否需要包装Frame的内容。 / p>

我编写了下面的函数,该函数使用网格方法智能地将小部件插入到框架中。它当然可以改进,但它适用于我。通常,如果使用.grid()填充父级,则填充小部件的宽度变化时,可能会浪费空白空间。下面的函数通过将父分解为逻辑列(代码中的divisions)并允许小部件跨越多个列来解决此问题。结果是网格布局,大大减少了浪费的空白区域。设置divisions越高,结果越好,但默认值应适用于大多数情况。

def smart_grid(parent, *args, **kwargs): # *args are the widgets!
    divisions   = kwargs.pop('divisions', 100)
    force_f     = kwargs.pop('force', False)
    if 'sticky' not in kwargs:
        kwargs.update(sticky='w')
    try:
        parent.win_width
    except:
        parent.win_width = -1
    winfo_width = parent.winfo_width()
    if 1 < winfo_width != parent.win_width or force_f:
        parent.win_width = winfo_width
        row = col = width = 0
        argc = len(args)
        for i in range(argc):
            widget_width = args[i].winfo_width()
            columns = max(1, int(widget_width * float(divisions) / winfo_width))
            width += widget_width
            if width > winfo_width:
                width = widget_width
                row += 1
                col = 0
            args[i].grid(row=row, column=col, columnspan=columns, **kwargs)
            col += columns
        parent.update_idletasks() # update() # 
        return row + 1

最终我会花几分钟时间用w.winfo_reqwidth()重写它,并会更新我的帖子。