【问题标题】:Iteration to place widgets in Tkinter and Pmw在 Tkinter 和 Pmw 中放置小部件的迭代
【发布时间】:2016-03-16 18:36:32
【问题描述】:

我通过在 Python 中使用循环在我的项目中创建、格式化和存储 Pmw EntryFields 而不是构建单独的小部件,从而大大减少了重复代码的数量。我什至设法将它们以线性方式放置在根窗口上——在第 0 列中一个在另一个之下。

但是,当我想将它们放在两列中时,我找不到生成以下小部件行和列坐标的方法

(0,0)(0 1),(1,0)(1,1)(2,0)(2,1) 来自 for 循环。我使用了一种解决方法,将上述坐标输入到 Python 列表中并引用该列表。

import tkinter
import Pmw
import time



root=tkinter.Tk()                                         #Create a main GUI window

root.option_readfile('optionDB')                          #Override Windows default fonts
##root.resizable(width=False, height=False)                #Fix the window size in stone
root.iconbitmap('logo.ico')                               #add logo
titles=['a', 'b','c', 'd','e','f']                        #List ttitles of entry fields
**locations=[(0,0),(0,1),(1,0),(1,1),(2,0),(2,1)]**           #Entry locations
entry ={}                                                 #Container for referencing entry fieleds



for nextWidget in range(len(titles)):                    #Buils widgets on the page                        

    dummy_x= Pmw.EntryField(root,                                           #widget properties
                               labelpos = 'w',
                               label_text = titles[nextWidget],
                               label_fg="white",
                               entry_width=10
                           )
    Pmw.Color.changecolor(dummy_x.component("hull"),
                                 background="blue")

    **dummy_x.grid(row= locations[nextWidget][0],**
                      column=locations[nextWidget][1],
                      padx=10,pady=10
                )

    entry.update({titles[nextWidget]: dummy_x})                             #add to container


root.mainloop()        #run the venet loop

您可以在主 for 循环中看到我遍历 名为位置的列表 并将值放置在 grid() 选项的行、列属性中以放置小部件。

问题: 要完全消除位置列表,有一种方法可以生成序列 (0,0)(0 1),(1,0)(1,1)(2,0)(2,1 ) 来自 for 循环本身?

【问题讨论】:

    标签: user-interface python-3.x tkinter refactoring pmw


    【解决方案1】:

    所有情况的一般答案是您可以使用 for()

    for this_row in range(3):
        for this_column in range(2):
             print this_row, this_column
    

    或递增计数器

    this_row=0
    this_column=0
    for nextWidget in range(len(titles)): 
        ...
        this_column += 1
        if this_column > 1:
            this_row += 1
            this_column = 0
    

    或测试 nextWidget%2 = 与上面类似,但 divmod 比模数更好

    for ctr in range(6):
        print divmod(ctr, 2)
    

    【讨论】:

    • 您是从头开始解决这个问题的,还是以前遇到过?如果你弄清楚了,你能告诉我你是怎么解决的,因为我不好意思说我放弃了!我已经想到了 mod 的想法,但从未想过将它与 div 配对!谢谢
    • 使用 divmod 方法重构了代码后,我开始认为我的原始代码不那么神秘了——有什么意见吗?
    • "lesscryptic" 取决于谁在维护它是一种价值判断。恕我直言,这是直截了当的,如果有人不熟悉 divmod,可以打印结果。
    • 另请注意,您的解决方案不是通用的,即它只适用于一个特定的组。
    • 是的,但我经常发现在制作 ďatabase 解决方案时,我主要只使用两种模式,即一列或两列 guis。感谢您的意见,这将有助于激发我课堂上的讨论。
    【解决方案2】:

    (i/2, i%2) (python2) 或 (i//2, i%2) (python3) 会给你你想要的数字,如果你只需要两列

    $ python3 -i
    Python 3.4.0 (default, Jun 19 2015, 14:20:21) 
    [GCC 4.8.2] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> for i in range(10):
    ...     print((i//2, i%2))
    ... 
    (0, 0)
    (0, 1)
    (1, 0)
    (1, 1)
    (2, 0)
    (2, 1)
    (3, 0)
    (3, 1)
    (4, 0)
    (4, 1)
    >>> 
    

    【讨论】:

    • 谢谢,我勾选了 Curly Jo,因为他是最古老的帖子,但我同样喜欢所有答案,它不允许我勾选两个答案。
    • @TimothyLawman:老实说,我不在乎。选择一个有帮助的。
    • 我有一个问题已经发布了一年多没有人回答 - 如何通过按钮将 Tkinter 文本小部件的内容发送到本地打印机?你有没有在日常工作中这样做过?
    • @TimothyLawman:不,我已经十年没有在工作中使用过打印机了。我假设有用于处理打印机的 python 库,但我从未使用过它们。
    • 好主意,我会看看有没有图书馆,我从来没想过这样做
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-13
    • 2019-12-29
    相关资源
    最近更新 更多