模拟投币试验

# -*- coding: utf-8 -*-
# Time    : 2019/6/11 13:52
# Author  : Eric
# FileName: 模拟投币试验.py
# Software: PyCharm
#-----------------------------------------------------------------------------------------------------------------------

import random
from tkinter import *


class Window:
    def __init__(self, title='游戏', width=300, height=120, staFunc=bool, stoFunc=bool):
        self.w = width
        self.h = height
        self.stat = True
        self.staFunc = staFunc
        self.stoFunc = stoFunc
        self.staIco = None
        self.stoIco = None
        self.root = Tk(className=title)


    def drawCenter(self):
        ws = self.root.winfo_screenwidth()  # 用户屏幕宽度
        hs = self.root.winfo_screenheight()  # 用户屏幕高度
        x = int((ws / 2) - (self.w / 2))  # 距屏幕左边框的像素点数
        y = int((hs / 2) - (self.h / 2))  # 距屏幕上边框的像素点数
        self.root.geometry('{}x{}+{}+{}'.format(self.w, self.h, x, y))


    def createWidgets(self):
        Label(self.root, text="实验次数:").grid(row=0, sticky=E)
        Label(self.root, text="正面出现的次数:").grid(row=1, sticky=E)
        Label(self.root, text="出现正面频率:").grid(row=2, sticky=E)
        self.e1 = Entry(self.root)
        self.hc = StringVar()
        self.e2 = Entry(self.root, textvariable=self.hc)
        self.p = StringVar()
        self.e3 = Entry(self.root, textvariable=self.p)
        self.e1.grid(row=0, column=1)
        self.e2.grid(row=1, column=1)
        self.e3.grid(row=2, column=1)
        self.btnSer = Button(self.root, command=self.click, width=3, height=1, text='运行')
        self.btnSer.grid(row=3, column=1, sticky=E)
        btnQuit = Button(self.root, text='关闭窗口', command=self.root.quit, width=8, height=1)
        btnQuit.grid(row=3, column=2)


    def click(self):
        h = 0  # 正面次数
        t = 0  # 反面次数
        allcount = 0
        count = int(self.e1.get())
        for i in range(count):
            num = random.randint(0, 1)
            if num == 0:
                h = h + 1
            else:
                t = t + 1
            allcount = allcount + 1
        print(allcount)
        self.hc.set(str(h))  # 正面次数
        self.p.set(str(h / count))  # 正面概率


    def loop(self):
        self.root.resizable(False, False)  # 禁止修改窗口大小
        self.createWidgets()
        self.drawCenter()  # 窗口居中
        self.root.mainloop()


# 你写的脚本模块既可以导入到别的模块中用,另外该模块自己也可执行
if __name__ == '__main__':
    w = Window(width=350, height=150)
    w.loop()

  

转载于:https://www.cnblogs.com/lsyb-python/p/11011236.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值