在for循环中设置python代码的行限制?

时间:2015-08-16 20:25:08

标签: python tkinter

问题:Tkinter.Label循环中达到一定数量的行后,如何更改for的行?

代码:

import Tkinter
import sys
from fractions import gcd


def func(event):
    x = int(e1.get())  # get max number
    result = []
    for a in range(1, x):  # loops to get each value in range of x
        for b in range(a, x):
            for c in range(b, x):
                if a**2 + b**2 == c**2 and gcd(a, b) == 1:  # if it is a primitive pyth triple, append result
                    result += ['[',a,',',b,',',c,']']  # add group of triples to list
    l = Tkinter.Message(root, text=result).grid(ipadx=5, ipady=5, sticky='W''E')  # display each group of triples to root
    l0 = Tkinter.Label(root, text="Non-primitive and primitive triples").grid(ipadx=5, ipady=5, sticky='W''E')
    root.bind('<Return>', close)  # Hit enter to exit, only temp for debugging, will reassign to button later


def close(event):  # close program, define parameter event to allow for binding
    Tkinter.sys.exit(0)
    sys.exit(0)


root = Tkinter.Tk()  # establish main gui
root.title('Generator')
e1 = Tkinter.Entry(root)
assert isinstance(e1, object)  # only method I've found to allow for Entry().grid()
e1.grid(ipadx=5, ipady=5, sticky='W''E')
root.bind('<Return>', func)  # bind to Enter, cleaner and quicker than a button
root.mainloop()

所以,如果我有一个显示的窗口

[#,#,#]

[#,#,#]

[#,#,#]

[#,#,#]

我如何将其更改为,如果我希望行限制为4(由于条目小部件,从第一列的第二行开始,因此第二列将比第一列多一个,并且对于每个连续的列保持不变),这:

输入 ..... [#,#,#]

[#,#,#] ... [#,#,#]

[#,#,#] ... [#,#,#]

[#,#,#] ... [#,#,#]

我希望l = Tkinter.Message(root, text=result).grid(ipadx=5, ipady=5, sticky='W''E') # display each group of triples to root位于for循环的if语句中,并将其更改为l = Tkinter.Label(root, text=('[',a,',',b,',',c,']')).grid(ipadx=5, ipady=5, sticky='W''E') # display each group of triples to root

更新

我在这里用一些帮助修改了代码:

import Tkinter
import sys
from fractions import gcd


def func(event):
    x = int(e1.get())  # get max number
    row = 0
    column = 0
    count = 0
    for a in range(1, x):  # loops to get each value in range of x
        for b in range(a, x):
            for c in range(b, x):
                if a**2 + b**2 == c**2 and gcd(a, b) == 1:  # if it is a primitive pyth triple, print
                    row += 1
                    l = Tkinter.Label(root, text=('[',a,',',b,',',c,']'))
                    assert isinstance(l, object)
                    l.grid(row=row, column=column, ipadx=5, ipady=5, sticky='W''E')  # display each group of triples to root
                    root.title('Primitive Triples')
                    if count > 1:
                        l.destroy()
                    if row == 7:
                        column += 1
                        row -= 8


def close():  # close program
    Tkinter.sys.exit(0)
    sys.exit(0)


root = Tkinter.Tk()  # establish main gui
root.title('Generator')
e1 = Tkinter.Entry(root)
assert isinstance(e1, object)  # only method I've found to allow for Entry().grid()
e1.grid(ipadx=5, ipady=5, sticky='W''E')
root.bind('<Return>', func)  # bind to Enter, cleaner and quicker than a button
root.mainloop()

1 个答案:

答案 0 :(得分:0)

只需在第0行,第0列,第0行第1列,第1列,第1行第0列,第0列等中输入网格。问题是什么?