本文介绍了什么是比 .grid() 更好的 Tkinter 几何管理器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的投诉

我目前对 Tkinter GUI 的研究比以往任何时候都更深入,我发现 .grid() 几何管理器由于以下几个原因而不够用:

I am currently delving deeper than "ever" before into the Tkinter GUI, and I have found the .grid() geometry manager to be inadequate for several reasons:

  1. 绘图基于其中最大的小部件 - 这种相对性会导致不准确.

  1. The plots are based on the largest widget within them - this relativity leads to inaccuracy.

在 Windows 7 中,在 Python 2.7.3 上,程序似乎没有注意我的行号,而是更喜欢使用小部件的顺序.

In Windows 7, on Python 2.7.3, the program does not seem to heed my row numbers, instead preferring to use the order of the widgets.

我的代码

我目前正在开发一个非常基本的文本编辑器,我希望在框架的顶部有多个按钮.我一直无法做到这一点,因为我的小部件被放置在占据屏幕中心的巨大文本框的最左侧或最右侧.

I am currently working on a really basic text editor, and I want to have multiple buttons on the top of the frame. I have been unable to do this as my widgets are placed either to the far left or right of the massive textbox that dominates the center of the screen.

========Class __init__ Stuff============
def widget(self):#Place widgets here

    #Save Button
    self.saveButton = Button (self, text = "Save", command = self.saveMe)
    self.saveButton.grid(column = 0, row = 0, sticky = W)

    #Open Button
    self.openButton = Button (self, text = "Open", command = self.openMe)
    self.openButton.grid(column = 0, row = 1, sticky = W)
    #Area where you write
    self.text = Text (self, width = (root.winfo_screenwidth() - 20),
                      height = (root.winfo_screenheight() - 10))
    self.text.grid(row = 2)
==============Mainloop/Command Stuff============

我的问题

是否有另一种更准确的方式使用 .grid() 几何管理器,或者我应该完全使用另一个函数?

Is there another way to use the .grid() geometry manager in a way that is more accurate, or should I be using another function altogether?

谢谢!

推荐答案

您可以使用 3 种几何管理器 -- gridpack地方.第三种是最通用的,但也很难使用.我更喜欢 grid.请注意,您可以将小部件放置在其他小部件内部——或者您可以指定 columnspan.因此,如果您想获得以下布局:

There are 3 geometry managers that you have available to you -- grid, pack and place. The third is the most general, but also very difficult to use. I prefer grid. Note that you can place widgets inside of other widgets -- Or you can specify columnspan. So, if you want to get the following layout:

  -------------------------
  |    Button1  | Button2 |
  -------------------------
  |     Big Widget        |
  -------------------------

使用 .grid 有 2 种规范的方法可以做到这一点.第一种方法是columnspan:

there are 2 canonical ways to do it using .grid. The first method is columnspan:

import Tkinter as Tk
root = Tk.Tk()
b1 = Tk.Button(root,text="Button1")
b1.grid(row=0,column=0)
b2 = Tk.Button(root,text="Button2")
b2.grid(row=0,column=1)
big_widget = Tk.Canvas(root)
big_widget.grid(row=1,column=0,columnspan=2)

*注意还有一个完全类似的 rowspan 选项.

*note that there is a completely analogous rowspan option as well.

第二种方法是使用 Frame 来固定按钮:

The second method is to use a Frame to hold your buttons:

import Tkinter as Tk
root = Tk.Tk()
f = Tk.Frame(root)
f.grid(row=0,column=0)
#place buttons on the *frame*
b1 = Tk.Button(f,text="Button1")
b1.grid(row=0,column=0)
b2 = Tk.Button(f,text="Button2")
b2.grid(row=0,column=1)

big_widget = Tk.Canvas(root)
big_widget.grid(row=1,column=0)  #don't need columnspan any more.

这种方法对于创建复杂的布局SUPER很有用——我不知道如何在不使用像这样的 Frame 对象的情况下创建复杂的布局...

This method is SUPER useful for creating complex layouts -- I don't know how you could create a complex layout without using Frame objects like this ...

这篇关于什么是比 .grid() 更好的 Tkinter 几何管理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 00:39