【发布时间】:2016-03-15 02:05:06
【问题描述】:
我创建了一个函数,它接受两个参数,打印出多个语句并最终返回一个答案。它在 python shell 中运行良好。
我正在使用 tkinter (python 3.4.1) 创建一个用户友好的程序,供消费者使用我的功能。我希望我的函数将所有内容输出到某个东西(我正在使用列表框),然后用户可以滚动浏览这些语句。但是,它只输出返回值,而不是打印语句。为什么??
这是我的代码,我认为问题出在 PageOne 类中。
将 tkinter 导入为 tk 从 tkinter 导入 ttk 从集合导入素数 从集合导入 isprim 从集合导入 LS 导入系统
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()
【问题讨论】:
标签: function class printing tkinter