写了一个小的随机数。我希望每个随机循环的结果以表格的形式显示在文本文档中。我无法以任何方式创建表并通过代码执行活动来补充它。
import random
import os
import tkinter as tk
from tkinter.ttk import Label, Button
from unittest import result #import all modules
root = tk.Tk()
root.title("Random")
root.geometry('400x200')
root.resizable(False, False) #create UI
rand = 0
result_ = 0
text_insert = 0
columns = ['Rand','Answer','Result']
file = 'C:/Users/YaroslavPolishchuk/Documents/Code/Project 1/app/'
with open('Shore.txt', 'w') as fp:
pass
def to_rand(): #function to rundom
global rand
global text_insert
rand=random.randint(1,10)
text_insert=textBoxx.get('1.0','end-1c')
label.config(text=rand)
if rand == int(text_insert):
label.config(text="WINN")
print("WINN")
label = tk.Label(
root,
text=rand,
font=("Helvetica", 14) #label with random number
)
label.pack(ipadx=10,
ipady=10
)
textBoxx = tk.Text(root,
height = 1,
width = 10
) #insert your text
textBoxx.pack(
ipadx=10,
ipady=10
)
textBoxx.insert('1.0','number')
random_button = tk.Button(
root,
text="random",
command=to_rand
) #Button to random
random_button.pack(
ipadx=5,
ipady=5,
)
root.mainloop()
最好使用像 texttable 这样的模块
关于集成的问题,初始化表,每次调用随机函数,添加数据,当脚本退出时,将数据保存到文件中,即使打印相同