我试图搜索这个主题,但它一直很混乱,因为我不使用/非常了解stdout / stderr ...但是,我现在有这个代码将列表打印到控制台。有人可以向我解释如何将其直接打印到GUI文本框上吗?我的代码:
from tkinter import *
import math
class TimeGenerator:
def __init__(self,master):
frame = Frame(master)
frame.grid()
label_iso = Label(root, text="Isotope A, Element")
label_vol = Label(root, text="Voltage")
label_range = Label(root, text="Charge Range")
entry_iso = Entry(root)
entry_vol = Entry(root)
entry_range = Entry(root)
label_iso.grid(row=0, sticky=E)
label_vol.grid(row=1, sticky=E)
label_range.grid(row=2, sticky=E)
entry_iso.grid(row=0, column=1)
entry_vol.grid(row=1, column=1)
entry_range.grid(row=2,column=1)
button = Button(root, text='Time Range', command=self.calculateTime)
button.grid(row=3, columnspan=2)
self.iso = entry_iso
self.vol = entry_vol
self.r = entry_range
def calculateTime(self):
x = 5
self.iso = self.iso.get().replace(" ", "")
list = []
for e in self.iso.split(","):
list.append(e)
f = open("/Users/LazyLinh/PycharmProjects/mass.mas12.txt", "r")
i = 0
while (i < 40):
header = f.readline()
i += 1
self.mass = 0
for line in f:
line = line.strip()
columns = line.split()
if (list[0] == columns[3]):
if (list[1] == columns[4]):
if (len(columns) == 16):
self.mass = float(columns[13].replace("#","")) + float(columns[14].replace("#",""))
else:
self.mass = float(columns[12].replace("#","")) + float(columns[13].replace("#",""))
self.r = self.r.get().replace(" ", "")
tup = tuple(int(x) for x in self.r.split(","))
list = []
for q in range(tup[0], tup[1] + 1):
y = x * math.sqrt(self.mass / (2 * q * float(self.vol.get())))
list.append(y)
i = tup[0]
for time in list:
print(i, ':', time)
i = i + 1
root = Tk()
b = TimeGenerator(root)
root.mainloop()
谢谢!
答案 0 :(得分:0)
在代码中的某处,您需要创建一个文本小部件:
class TimeGenerator:
def __init__(self,master):
...
self.text = Text(...)
...
稍后,使用文本小部件的insert
方法而不是print
语句:
for time in list:
self.text.insert("end", "%d: %s\n" % (i, time))
i = i + 1