[英]How would I remove labels in my for loops
So, how would I remove all the Tkinter.Label
's generated within my for
loop and generate new ones while retaining my method of generation? 那么,我如何删除在for
循环中生成的所有Tkinter.Label
并生成新的Tkinter.Label
,同时保留我的生成方法? Cause if I want to lower the number it won't remove the labels of the previous number(Such as, input '6' and you get [3,4,5] and the next number '14' generates [5,12,13]
. How could I remove [5,12,13]
if my input < 14
?), if someone has a better method of generating the output for these labels it would be greatly appreciated if you would try and educate me a little. 原因是,如果我想降低数字,它将不会删除前一个数字的标签(例如,输入“ 6”,您会得到[3,4,5],而下一个数字“ 14”会生成[5,12,13]
。如果我的input < 14
?我该如何删除[5,12,13]
,如果有人有更好的方法来生成这些标签的输出,那么如果您尝试一点教育我,将不胜感激。 Here's the code: 这是代码:
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()
Here, I think this is what you wanted: (if python 2, replace import tkinter with import Tkinter as tkinter) 在这里,我想这就是您想要的:(如果是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()
What has changed from the code originally posted is as follows: 与最初发布的代码相比,变化如下:
The original code attempted to do all that in one function; 原始代码试图在一个功能中完成所有这些工作; it was failing to discard old results; 它没有放弃旧的结果; the consequence was that (1) the display was an odd mix of older and newer results (inaccurate and inexact), and (2) discarded, but never destroyed widgets were cluttering the app space. 结果是(1)显示结果是旧结果和较新结果(不准确和不精确)的奇怪混合,并且(2)被丢弃,但从未销毁的小部件会阻塞应用程序空间。
On OSX, the widget looks like this: 在OSX上,小部件如下所示:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.