我创建了一个函数,它接受两个参数,打印多个语句并最终返回一个答案。它在python shell中运行良好。
我正在使用tkinter(python 3.4.1)为消费者创建一个用户友好的程序来使用我的功能。我希望我的函数输出所有内容(我使用的是列表框),然后用户可以滚动查看语句。但是,它只输出返回值,而不是print语句。为什么?
这是我的代码,我相信问题出在PageOne类中。
将tkinter导入为tk 来自tkinter import ttk 来自Collections import primes 来自Collections import isprime 来自Collections import LS import sys
LARGE_FONT = ("Verdana", 12)
NORM_FONT = ("Verdana", 10)
SMALL_FONT = ("Verdana", 8)
def popupmsg(msg):
popup = tk.Tk()
popup.wm_title("!")
label = ttk.Label(popup, text=msg, font=NORM_FONT)
label.pack(side="top", fill="x", pady = 10)
B1 = ttk.Button(popup, text="Okay", command = popup.destroy)
B1.pack()
popup.mainloop()
class Mathapp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.iconbitmap(self, default="psi.ico")
tk.Tk.wm_title(self, "Math App")
container = tk.Frame(self)
container.pack(side="top", fill="both", expand = True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
menubar = tk.Menu(container)
filemenu = tk.Menu(menubar, tearoff = 0)
filemenu.add_command(label = "Save settings", command = lambda: popupmsg("Not supported just yet!"))
filemenu.add_separator()
filemenu.add_command(label = "Exit", command = quit)
menubar.add_cascade(label = "File", menu=filemenu)
prog = tk.Menu(menubar, tearoff = 0)
prog.add_command(label = "Legendre Symbol", command = lambda: popupmsg("Not supported just yet!"))
prog.add_command(label = "Prime Sieve", command = lambda: popupmsg("Not supported just yet!"))
prog.add_command(label = "Prime Factorisation - Sieve", command = lambda: popupmsg("Not supported just yet!"))
menubar.add_cascade(label = "Programs", menu=prog)
tk.Tk.config(self, menu=menubar)
self.frames = {}
for F in (StartPage, PageOne, PageTwo, PageThree):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Start Page", font=LARGE_FONT)
label.pack(pady = 10, padx = 10)
button1 = ttk.Button(self, text="Legendre Symbol",
command = lambda: controller.show_frame(PageOne))
button1.pack()
button2 = ttk.Button(self, text="Prime test - Sieve",
command = lambda: controller.show_frame(PageTwo))
button2.pack()
button3 = ttk.Button(self, text="Prime Factorisation",
command = lambda: controller.show_frame(PageThree))
button3.pack()
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Legendre Symbol", font=LARGE_FONT)
label.pack(pady = 10, padx = 10)
button1 = ttk.Button(self, text="Back to home",
command = lambda: controller.show_frame(StartPage))
button1.pack()
##################
def show_answer():
Ans = str(LS(int(num1.get()),int(num2.get())))
listbox.delete(0,"end")
listbox.insert(0, LS(int(num1.get()),int(num2.get())))
tk.Label(self, text = "Enter the numbers to be tested ").pack()
num1 = tk.Entry(self)
num1.pack()
num2 = tk.Entry(self)
num2.pack()
blank = tk.Scrollbar(self)
blank.pack()
listbox= tk.Listbox(self, yscrollcommand=blank.set)
listbox.pack()
tk.Button(self, text = "Test", command = show_answer).pack()
##################
app = Mathapp()
app.mainloop()
答案 0 :(得分:0)
看起来这个问题可能就在这里:
def show_answer():
Ans = str(LS(int(num1.get()),int(num2.get())))
listbox.delete(0,"end")
listbox.insert(0, LS(int(num1.get()),int(num2.get())))
我建议从命令行创建几个条目小部件,并修改此函数,直到您可以获取并打印条目的内容。您可能在Ans
和listbox.insert
行中包含太多内容;尝试将不同的步骤分成不同的代码行。当您获得条目小部件内容时,您可以尝试一次一个地插入列表框的end
索引(当然,如果其中没有任何内容,则会0
,但{ {1}}如果已有项目。)