gpt4 book ai didi

python - 如何删除 for 循环中的标签

转载 作者:太空宇宙 更新时间:2023-11-03 17:28:21 26 4
gpt4 key购买 nike

那么,我如何删除所有 Tkinter.Label是在我的 for 中生成的循环并生成新的,同时保留我的生成方法?因为如果我想降低数字,它不会删除前一个数字的标签(例如,输入“6”,你得到 [3,4,5] ,下一个数字“14”生成 [5,12,13] 。如何我可以删除 [5,12,13] 如果我的 input < 14 吗?),如果有人有更好的方法来生成这些标签的输出,如果您愿意尝试教育我一点,我将不胜感激。代码如下:

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()

最佳答案

在这里,我认为这就是您想要的:(如果 python 2,将 import tkinter 替换为 import Tkinter as tkinter)

import tkinter
import sys
from fractions import gcd

CURRENT_LABELS = []

def pythagorean_primitive(a, b, c):
"""returns True if a,b,c are pythagorean primitives, False otherwise"""
return a**2 + b**2 == c**2 and gcd(a, b) == 1

def generate_results(n):
"""lists each triplet of distinct integers <n that is a pythagorean primitive"""
results = []
for a in range(1, n):
for b in range(a, n):
for c in range(b, n):
if pythagorean_primitive(a, b, c):
results.append([a, b, c])
return results

def generate_labels(sequence):
"""returns a list of tkinter labels from the sequence provided"""
labels = []
for elt in sequence:
a, b, c = elt[0], elt[1], elt[2]
labels.append(tkinter.Label(root, text='[' + str(a) + ', '+ str(b) + ", " + str(c) + "]"))
return labels

def destroy_old():
"""purges the current tkinter labels from root, and destroys them"""
global CURRENT_LABELS
for elt in CURRENT_LABELS:
elt.grid_forget()
elt.destroy()

def show_new_labels(sequence):
"""assembles a new display of tkinter labels from the sequence provided"""
r, c = 1, 0
for label in sequence:
label.grid(row=r, column=c, ipadx=5, ipady=5, sticky='W''E')
r += 1
if not r % 10:
r = 1
c += 1

def event_handler(event):
"""deals with the input of a number in the Entry field"""
global CURRENT_LABELS
x = int(e1.get()) # get max number
results = generate_results(x)
try:
destroy_old()
except IndexError:
pass
CURRENT_LABELS = generate_labels(results)
show_new_labels(CURRENT_LABELS)


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>', event_handler) # bind to Enter, cleaner and quicker than a button
root.mainloop()

与最初发布的代码相比,更改内容如下:

  • 将庞大函数中的每个连贯步骤重构为单独的和大部分是独立的功能,具有更 Eloquent 名称。变量的名称也已更改为更具可读性。
  • 因此,结果的计算与显示格式无关。
  • 结果汇总在一个列表中
  • 列表被传递以创建 tkinter.Labels 准备显示;这些标签聚合在一个列表中。
  • 显示先前计算结果的(旧)​​标签随后将从显示中删除并销毁。
  • 随后会显示保存新计算结果的新标签。

原始代码试图在一个函数中完成所有这些工作;它未能丢弃旧的结果;结果是(1)显示是旧结果和新结果的奇怪混合(不准确和不精确),以及(2)被丢弃但从未被破坏的小部件使应用程序空间变得困惑。

在 OSX 上,小部件如下所示:

results for entry = 16

results for entry = 200

关于python - 如何删除 for 循环中的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32264558/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com