函数的print输出要在gui界面显示,请问要怎么搞丫
下附我参考进行修改的代码,可以改成不是类的么,虽说我其实看不懂
import tkinter as tk
import sys
class ExampleApp(tk.Tk):
def __init__(self):
super(ExampleApp, self).__init__()
toolBar = tk.Frame(self)
toolBar.pack(side=tk.TOP, fill=tk.X)
button1 = tk.Button(self, text='打印乘法表', command=self.print_stdout)
button1.pack(in_=toolBar, side=tk.BOTTOM)
self.text = tk.Text(self, wrap='word')
self.text.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
sys.stdout = TextRedirector(self.text)
def print_stdout(self):
for i in range(1, 10):
for j in range(1, i + 1):
print('%s*%s=%s' % (i, j, i * j), end=' ')
print()
class TextRedirector(object):
def __init__(self, widget, tag='stdout'):
self.widget = widget
self.tag = tag
def write(self, str):
self.widget.configure(state='normal')
self.widget.insert(tk.END, str, (self.tag,)) # (self.tag,) 是设置配置
self.widget.configure(state='disabled')
if __name__ == '__main__':
app = ExampleApp()
app.mainloop()
我目前的代码,改了很久,但是还是在gui打印不出来乘法表
import sys
import tkinter as tk
from tkinter import Tk
from tkinter import messagebox
from tkinter.messagebox import *
import random
import datetime
import time
class TextRedirector(object):
def __init__(tk, widget, tag='stdout'):
tk.widget = widget
tk.tag = tag
def write(tk, str):
tk.widget.configure(state='normal')
tk.widget.insert(tk.END, str, (self.tag,)) # (self.tag,) 是设置配置
tk.widget.configure(state='disabled')
window = tk.Tk()
score = 0
count = 1
a = random.randint(1, 9)
b = random.randint(1, 9)
mul01 = tk.StringVar()
mul01.set(str(a))
mul02 = tk.StringVar()
mul02.set(str(b))
count_title = tk.StringVar()
count_title.set('第1道题')
score_lab = tk.StringVar()
score_lab.set('0')
answer_lab = tk.StringVar()
gameove = tk.StringVar()
t=tk.StringVar()
t1=tk.StringVar()
time_solve = tk.DoubleVar()
gameove.set("共测试10道乘法,每题10分,总分100分")
window.title("乘法口诀测试")
window.geometry('600x500')
window["background"] = "LightCyan"
title01 = tk.Label(window,
textvariable=gameove,
font=('华文行楷', 15),
bg='SpringGreen')
title01.place(x=120, y=30)
label01 = tk.Label(window,
textvariable=count_title,
font=('华文行楷', 20),
bg='hotpink') # 第N道题
label01.place(x=100, y=100)
title02 = tk.Label(window, text="请输入答案:", font=('华文行楷', 20),
bg='hotpink')
title02.place(x=80, y=250)
title03 = tk.Label(window,
textvariable=score_lab,
font=('华文行楷', 28),
bg='hotpink') # 分数lab
title03.place(x=340, y=380)
title04 = tk.Label(window, text="得分:", font=('华文行楷', 28), bg='hotpink') # 得分lab
title04.place(x=190, y=380)
title05 = tk.Label(window, textvariable=answer_lab, font=('华文行楷', 28), bg='hotpink') # 得分lab
title05.place(x=220, y=450)
entry01 = tk.Entry(window, textvariable=0, width=10, font=('华文行楷', 20), show=None) # 文本输入框
entry01.place(x=300, y=250)
label02 = tk.Label(window, textvariable=mul01, font=('华文行楷', 28),
bg='hotpink') # 被乘数lab
label02.place(x=180, y=175)
label03 = tk.Label(window, textvariable=mul02, font=('华文行楷', 28),
bg='hotpink') # 乘数lab
label03.place(x=340, y=175)
imgLabel = tk.Label(window, text='X', font=('黑体', 28),
bg='hotpink') # 乘号标签
imgLabel.place(x=263, y=170)
def multitable():
for i in range(1, 10):
for j in range(1, i + 1):
print('%s*%s=%s' % (i, j, i * j), end=' ')
print()
# 当点击右上角退出时,执行的程序
def on_closing():
if messagebox.askokcancel("Quit", "Do you want to quit?"):
sys.exit()
def surecallback():
global a, b, count,startTime,endTime,time_solve
if count <= 10:
product = a * b
result = int(entry01.get())
if result == product:
global score
score = score + 10
score_lab.set(str(score))
answer_lab.set("答对了")
else:
answer_lab.set("答错了")
endTime = time.time()
time_solve = endTime - startTime
entry01.delete(0, tk.END)
a = random.randint(1, 9)
b = random.randint(1, 9)
mul01.set(str(a))
mul02.set(str(b))
count = count + 1
c = '第' + str(count) + '道题'
count_title.set(c)
else:
title01.destroy()
title02.destroy()
entry01.destroy()
title03.destroy()
title04.destroy()
title05.destroy()
label01.destroy()
label02.destroy()
label03.destroy()
imgLabel.destroy()
button01.destroy()
gameove.set("测试结束")
title06 = tk.Label(window,
textvariable=gameove, font=('华文行楷', 40),
bg='SpringGreen')
title06.place(x=220, y=30)
t.set('进行本次乘法测试花费的时间为:%s' % (time_solve))
t1.set('进行每次测试花费的平均时间为:%s' %(time_solve/10))
title07 = tk.Label(window,
textvariable=t, font=('华文行楷', 15),
bg='SpringGreen')
title07.place(x=30, y=140)
title08 = tk.Label(window,
textvariable=t1, font=('华文行楷', 15),
bg='SpringGreen')
title08.place(x=30, y=200)
toolBar = tk.Frame()
toolBar.pack(side=tk.TOP, fill=tk.X)
button1 = tk.Button( text='打印乘法表', command=multitable)
button1.pack(in_=toolBar, side=tk.BOTTOM)
text = tk.Text( wrap='word')
text.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
stdout = TextRedirector(text)
button03 = tk.Button(text='退出测试',
font=('华文行楷', 20),
width=14,
height=1,
command=on_closing)
button03.place(x=230, y=370)
window.mainloop()
button01 = tk.Button(text='确认',
font=('华文行楷', 20),
width=8,
height=1,
command=surecallback)
button01.place(x=230, y=310)
startTime = time.time()
print(startTime)
window.mainloop()