From 8ecb11e9ac8743ebd4abdc1c3073577aac0f194a Mon Sep 17 00:00:00 2001 From: link-fgfgui Date: Fri, 14 Jan 2022 15:13:56 +0800 Subject: [PATCH] Update main.py --- main.py | 118 ++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 72 insertions(+), 46 deletions(-) diff --git a/main.py b/main.py index 4365f03..b8e9c92 100644 --- a/main.py +++ b/main.py @@ -1,49 +1,75 @@ +# coding:utf-8 #欢迎使用![by link] import random import time -print('欢迎进入模拟试验!') -time.sleep(0.8) -print('说明:本程序仿照八下人教版生物书P41页模拟试验,如有条件,建议动手操作体验') -time.sleep(2.2) -print('数据说明:A指XY,B指XX,建议结合12月综合练习生物卷P6 T31理解') -time.sleep(2.5) -print('即将进行随机') -time.sleep(0.5) -in1=int(input('请输入抓取次数:\n')) -in2=int(input('请输入重复试验次数:\n')) -A = 0 -B = 0 -A1 = 'A' -B1 = 'B' -l1=[] -print('随机过程:') -start=time.perf_counter() -for i in range(in2): - print('----------第{}次重复试验----------'.format(i+1)) - for i in range(in1): - rand_int = random.randint(1,10000) - if rand_int <= 5000: - A = A+1 - print('<{}>'.format(A1), end='') - elif rand_int > 5000: - B = B+1 - print('<{}>'.format(B1), end='') - print() - print('----------结束----------') - print('结果:A{}次,B{}次'.format(A, B)) - l1.append([A,B]) - A=0 - B=0 -dur=time.perf_counter()-start -A=0 -B=0 -print('处理数据中,请稍后......') -start=time.perf_counter() -for i in range(len(l1)): - A=A+l1[i][0] - B=B+l1[i][1] -dur1=time.perf_counter()-start -time.sleep(1.8) -print('\n计算完毕,循环耗时{:.2f}s,计算耗时{:.5f}s'.format(dur,dur1)) -print('最终结果:\nA共{}次,B共{}次\n由数据可知,A:B基本为1:1,故生男生女概率相同。\n'.format(A,B)) -input('按Enter键关闭') \ No newline at end of file +from tkinter import * +from tkinter.ttk import * + +def clicked(): + global A + global A1 + global B + global B1 + global l1 + global out1 + global start + global dur + global dur1 + l1=[] + out='' + out1='' + A = 0 + B = 0 + A1 = 'A' + B1 = 'B' + in1=int(combo.get()) + in2=int(combo1.get()) + result_data_Text.delete(1.0,END) + result_data_Text1.delete(1.0,END) + start=time.perf_counter() + for i in range(in2): + out=out+'\n----------第{}次重复试验----------\n'.format(i+1) + for i in range(in1): + rand_int = random.randint(1,10000) + if rand_int <= 5000: + A = A+1 + out=out+'<{}>'.format(A1) + elif rand_int > 5000: + B = B+1 + out=out+'<{}>'.format(B1) + out=out+'\n----------结束----------\n' + out=out+'结果:A{}次,B{}次'.format(A, B) + l1.append([A,B]) + A=0 + B=0 + result_data_Text.insert(1.0,out) + dur=time.perf_counter()-start + start=time.perf_counter() + for i in range(len(l1)): + A=A+l1[i][0] + B=B+l1[i][1] + dur1=time.perf_counter()-start + result_data_Text1.insert(1.0,'\n计算完毕,循环耗时{:.2f}s,计算耗时{:.5f}s'.format(dur,dur1)) + result_data_Text1.insert(1.0,'最终结果:\nA共{}次,B共{}次\n由数据可知,A:B基本为1:1,故生男生女概率相同。\n'.format(A,B)) +window = Tk() +window.title("模拟试验") +window.geometry("700x800+300+3") +lbl = Label(window, text=" 抓取次数:") +lbl.grid(column=0, row=0) +lbl1 = Label(window, text=" 重复次数:") +lbl1.grid(column=0, row=8) +combo = Combobox(window) +combo1 = Combobox(window) +combo['values'] = (1,3,5,10,20,40,50,100,500,1000,1500) +combo1['values'] = (1,10,20,100,300,1000) +combo.current(2) +combo1.current(2) +combo.grid(column=8, row=0) +combo1.grid(column=8, row=8) +btn = Button(window, text="开始", command=clicked) +btn.grid(column=8, row=16) +result_data_Text = Text(window, width=70, height=13) +result_data_Text.grid(row=888, column=8) +result_data_Text1 = Text(window, width=70, height=29) +result_data_Text1.grid(row=100, column=8) +window.mainloop()