不导包的代码

from PyQt5 import QtCore, QtGui, QtWidgets
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import * class Ui_widget(object):
def setupUi(self, widget):
widget.setObjectName("widget")
widget.resize(548, 461) self.pushButton_1 = QtWidgets.QPushButton(widget)
self.pushButton_1.setGeometry(QtCore.QRect(60, 130, 111, 81))
self.pushButton_1.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(widget)
self.pushButton_2.setGeometry(QtCore.QRect(220, 130, 111, 81))
self.pushButton_2.setObjectName("pushButton_2")
self.pushButton_3 = QtWidgets.QPushButton(widget)
self.pushButton_3.setGeometry(QtCore.QRect(390, 130, 111, 81))
self.pushButton_3.setObjectName("pushButton_3")
self.pushButton_4 = QtWidgets.QPushButton(widget)
self.pushButton_4.setGeometry(QtCore.QRect(60, 240, 111, 81))
self.pushButton_4.setObjectName("pushButton_4")
self.pushButton_5 = QtWidgets.QPushButton(widget)
self.pushButton_5.setGeometry(QtCore.QRect(60, 340, 111, 81))
self.pushButton_5.setObjectName("pushButton_5")
self.pushButton_6 = QtWidgets.QPushButton(widget)
self.pushButton_6.setGeometry(QtCore.QRect(220, 240, 111, 81))
self.pushButton_6.setObjectName("pushButton_6")
self.pushButton_7 = QtWidgets.QPushButton(widget)
self.pushButton_7.setGeometry(QtCore.QRect(390, 240, 111, 81))
self.pushButton_7.setObjectName("pushButton_7")
self.pushButton_8 = QtWidgets.QPushButton(widget)
self.pushButton_8.setGeometry(QtCore.QRect(220, 340, 111, 81))
self.pushButton_8.setObjectName("pushButton_8")
self.pushButton_9 = QtWidgets.QPushButton(widget)
self.pushButton_9.setGeometry(QtCore.QRect(390, 340, 111, 81))
self.pushButton_9.setObjectName("pushButton_9")
self.pushButton_10 = QtWidgets.QPushButton(widget)
self.pushButton_10.setGeometry(QtCore.QRect(30, 20, 91, 41))
self.pushButton_10.setObjectName("pushButton_10")
self.pushButton_11 = QtWidgets.QPushButton(widget)
self.pushButton_11.setGeometry(QtCore.QRect(30, 80, 91, 41))
self.pushButton_11.setObjectName("pushButton_11")
self.label = QtWidgets.QLabel(widget)
self.label.setGeometry(QtCore.QRect(220, 30, 151, 51))
self.label.setObjectName("label") self.retranslateUi(widget)
QtCore.QMetaObject.connectSlotsByName(widget)
widget.setTabOrder(self.pushButton_1, self.pushButton_2)
widget.setTabOrder(self.pushButton_2, self.pushButton_4)
widget.setTabOrder(self.pushButton_4, self.pushButton_3)
widget.setTabOrder(self.pushButton_3, self.pushButton_5)
widget.setTabOrder(self.pushButton_5, self.pushButton_6)
widget.setTabOrder(self.pushButton_6, self.pushButton_7)
widget.setTabOrder(self.pushButton_7, self.pushButton_8)
widget.setTabOrder(self.pushButton_8, self.pushButton_9)
widget.setTabOrder(self.pushButton_9, self.pushButton_10)
widget.setTabOrder(self.pushButton_10, self.pushButton_11) def retranslateUi(self, widget):
_translate = QtCore.QCoreApplication.translate
widget.setWindowTitle(_translate("widget", "帅帅的打地鼠游戏"))
#self.pushButton.setText(_translate("widget", "PushButton"))
self.pushButton_1.setStyleSheet('QPushButton{background-image:url(tu.png)}')
self.pushButton_2.setStyleSheet('QPushButton{background-image:url(tu.png)}')
self.pushButton_3.setStyleSheet('QPushButton{background-image:url(tu.png)}')
self.pushButton_4.setStyleSheet('QPushButton{background-image:url(tu.png)}')
self.pushButton_5.setStyleSheet('QPushButton{background-image:url(tu.png)}')
self.pushButton_6.setStyleSheet('QPushButton{background-image:url(tu.png)}')
self.pushButton_7.setStyleSheet('QPushButton{background-image:url(tu.png)}')
self.pushButton_8.setStyleSheet('QPushButton{background-image:url(tu.png)}')
self.pushButton_9.setStyleSheet('QPushButton{background-image:url(tu.png)}')
self.pushButton_10.setText(_translate("widget", "开始游戏"))
self.pushButton_11.setText(_translate("widget", "停止"))
self.label.setText(_translate("widget", "TextLabel")) from PyQt5.QtCore import QTimer
import time
import numpy as np class MyWindow(QDialog, Ui_widget):
def __init__(self, parent=None):
super().__init__(parent)
self. setupUi(self)
self.begin = False
self.end = True
self.score = 0 #标记这9格子有没有出现小老鼠
self.flag = np.zeros(10) self.timer = QTimer(self) #初始化一个定时器
self.timer.timeout.connect(self.operate) #计时结束调用operate()方法 self.push_dic={}
self.push_dic[1]=self.pushButton_1
self.push_dic[2]=self.pushButton_2
self.push_dic[3]=self.pushButton_3
self.push_dic[4]=self.pushButton_4
self.push_dic[5]=self.pushButton_5
self.push_dic[6]=self.pushButton_6
self.push_dic[7]=self.pushButton_7
self.push_dic[8]=self.pushButton_8
self.push_dic[9]=self.pushButton_9 self.pushButton_10.clicked.connect(self.game)
self.pushButton_11.clicked.connect(self.gameover)
self.label.setText(str(self.score)) self.push_dic[1].clicked.connect(lambda:self.getscore(1))
self.push_dic[2].clicked.connect(lambda:self.getscore(2))
self.push_dic[3].clicked.connect(lambda:self.getscore(3))
self.push_dic[4].clicked.connect(lambda:self.getscore(4))
self.push_dic[5].clicked.connect(lambda:self.getscore(5))
self.push_dic[6].clicked.connect(lambda:self.getscore(6))
self.push_dic[7].clicked.connect(lambda:self.getscore(7))
self.push_dic[8].clicked.connect(lambda:self.getscore(8))
self.push_dic[9].clicked.connect(lambda:self.getscore(9)) self.label.setStyleSheet("font:20pt '楷体';border-width: 1px;border-style: solid;border-color: rgb(255, 0, 0);") # 每过一秒发生的事情
def operate(self):
#具体操作
#print("计时结束") #地鼠全部藏起来
for i in range(1,10):
self.push_dic[i].setStyleSheet('QPushButton{background-image:url(tu.png)}')
self.flag = np.zeros(10) a=np.random.randint(1,10)
self.push_dic[a].setStyleSheet('QPushButton{background-image:url(mouse.png)}')
self.flag[a]=1
a=np.random.randint(1,10)
self.push_dic[a].setStyleSheet('QPushButton{background-image:url(mouse.png)}')
self.flag[a]=1
a=np.random.randint(1,10)
self.push_dic[a].setStyleSheet('QPushButton{background-image:url(mouse.png)}')
self.flag[a]=1 def game(self):
if self.begin == False:
self.end = False
self.begin = True
print("启动计时器")
self.timer.start(800) #设置计时间隔并启动
self.pushButton_10.setText("暂停") elif self.begin == True:
self.begin = False
self.timer.stop()
self.pushButton_10.setText("继续") def gameover(self):
if self.end == False:
self.begin = False
self.end == True
self.pushButton_10.setText("开始游戏")
self.flag = np.zeros(10)
self.timer.stop()
#地鼠全部藏起来
for i in range(1,10):
self.push_dic[i].setStyleSheet('QPushButton{background-image:url(tu.png)}')
self.score = 0
self.label.setText(str(self.score)) def getscore(self,a):
if self.flag[a] == 1 and self.begin == True:
self.score += 1
self.label.setText(str(self.score))
self.flag[a] = 0
self.push_dic[a].setStyleSheet('QPushButton{background-image:url(catch.png)}') app = QApplication(sys.argv)
form = MyWindow()
form.show()
app.exec_()

导包的代码,先用Qt布局再

qt_designer.ui转为qt_designer.py以后

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file '11.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# WARNING! All changes made in this file will be lost! from PyQt5 import QtCore, QtGui, QtWidgets class Ui_widget(object):
def setupUi(self, widget):
widget.setObjectName("widget")
widget.resize(548, 461) self.pushButton_1 = QtWidgets.QPushButton(widget)
self.pushButton_1.setGeometry(QtCore.QRect(60, 130, 111, 81))
self.pushButton_1.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(widget)
self.pushButton_2.setGeometry(QtCore.QRect(220, 130, 111, 81))
self.pushButton_2.setObjectName("pushButton_2")
self.pushButton_3 = QtWidgets.QPushButton(widget)
self.pushButton_3.setGeometry(QtCore.QRect(390, 130, 111, 81))
self.pushButton_3.setObjectName("pushButton_3")
self.pushButton_4 = QtWidgets.QPushButton(widget)
self.pushButton_4.setGeometry(QtCore.QRect(60, 240, 111, 81))
self.pushButton_4.setObjectName("pushButton_4")
self.pushButton_5 = QtWidgets.QPushButton(widget)
self.pushButton_5.setGeometry(QtCore.QRect(60, 340, 111, 81))
self.pushButton_5.setObjectName("pushButton_5")
self.pushButton_6 = QtWidgets.QPushButton(widget)
self.pushButton_6.setGeometry(QtCore.QRect(220, 240, 111, 81))
self.pushButton_6.setObjectName("pushButton_6")
self.pushButton_7 = QtWidgets.QPushButton(widget)
self.pushButton_7.setGeometry(QtCore.QRect(390, 240, 111, 81))
self.pushButton_7.setObjectName("pushButton_7")
self.pushButton_8 = QtWidgets.QPushButton(widget)
self.pushButton_8.setGeometry(QtCore.QRect(220, 340, 111, 81))
self.pushButton_8.setObjectName("pushButton_8")
self.pushButton_9 = QtWidgets.QPushButton(widget)
self.pushButton_9.setGeometry(QtCore.QRect(390, 340, 111, 81))
self.pushButton_9.setObjectName("pushButton_9")
self.pushButton_10 = QtWidgets.QPushButton(widget)
self.pushButton_10.setGeometry(QtCore.QRect(30, 20, 91, 41))
self.pushButton_10.setObjectName("pushButton_10")
self.pushButton_11 = QtWidgets.QPushButton(widget)
self.pushButton_11.setGeometry(QtCore.QRect(30, 80, 91, 41))
self.pushButton_11.setObjectName("pushButton_11")
self.label = QtWidgets.QLabel(widget)
self.label.setGeometry(QtCore.QRect(220, 30, 151, 51))
self.label.setObjectName("label") self.retranslateUi(widget)
QtCore.QMetaObject.connectSlotsByName(widget)
widget.setTabOrder(self.pushButton_1, self.pushButton_2)
widget.setTabOrder(self.pushButton_2, self.pushButton_4)
widget.setTabOrder(self.pushButton_4, self.pushButton_3)
widget.setTabOrder(self.pushButton_3, self.pushButton_5)
widget.setTabOrder(self.pushButton_5, self.pushButton_6)
widget.setTabOrder(self.pushButton_6, self.pushButton_7)
widget.setTabOrder(self.pushButton_7, self.pushButton_8)
widget.setTabOrder(self.pushButton_8, self.pushButton_9)
widget.setTabOrder(self.pushButton_9, self.pushButton_10)
widget.setTabOrder(self.pushButton_10, self.pushButton_11) def retranslateUi(self, widget):
_translate = QtCore.QCoreApplication.translate
widget.setWindowTitle(_translate("widget", "帅帅的打地鼠游戏"))
#self.pushButton.setText(_translate("widget", "PushButton"))
self.pushButton_1.setStyleSheet('QPushButton{background-image:url(tu.png)}')
self.pushButton_2.setStyleSheet('QPushButton{background-image:url(tu.png)}')
self.pushButton_3.setStyleSheet('QPushButton{background-image:url(tu.png)}')
self.pushButton_4.setStyleSheet('QPushButton{background-image:url(tu.png)}')
self.pushButton_5.setStyleSheet('QPushButton{background-image:url(tu.png)}')
self.pushButton_6.setStyleSheet('QPushButton{background-image:url(tu.png)}')
self.pushButton_7.setStyleSheet('QPushButton{background-image:url(tu.png)}')
self.pushButton_8.setStyleSheet('QPushButton{background-image:url(tu.png)}')
self.pushButton_9.setStyleSheet('QPushButton{background-image:url(tu.png)}')
self.pushButton_10.setText(_translate("widget", "开始游戏"))
self.pushButton_11.setText(_translate("widget", "停止"))
self.label.setText(_translate("widget", "TextLabel"))

再导入

from qt_desiginer import Ui_widget
from PyQt5.QtCore import QTimer
import time
import numpy as np
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys class MyWindow(QDialog, Ui_widget):
def __init__(self, parent=None):
super().__init__(parent)
self. setupUi(self)
self.begin = False
self.end = True
self.score = 0 #标记这9格子有没有出现小老鼠
self.flag = np.zeros(10) self.timer = QTimer(self) #初始化一个定时器
self.timer.timeout.connect(self.operate) #计时结束调用operate()方法 self.push_dic={}
self.push_dic[1]=self.pushButton_1
self.push_dic[2]=self.pushButton_2
self.push_dic[3]=self.pushButton_3
self.push_dic[4]=self.pushButton_4
self.push_dic[5]=self.pushButton_5
self.push_dic[6]=self.pushButton_6
self.push_dic[7]=self.pushButton_7
self.push_dic[8]=self.pushButton_8
self.push_dic[9]=self.pushButton_9 self.pushButton_10.clicked.connect(self.game)
self.pushButton_11.clicked.connect(self.gameover)
self.label.setText(str(self.score)) self.push_dic[1].clicked.connect(lambda:self.getscore(1))
self.push_dic[2].clicked.connect(lambda:self.getscore(2))
self.push_dic[3].clicked.connect(lambda:self.getscore(3))
self.push_dic[4].clicked.connect(lambda:self.getscore(4))
self.push_dic[5].clicked.connect(lambda:self.getscore(5))
self.push_dic[6].clicked.connect(lambda:self.getscore(6))
self.push_dic[7].clicked.connect(lambda:self.getscore(7))
self.push_dic[8].clicked.connect(lambda:self.getscore(8))
self.push_dic[9].clicked.connect(lambda:self.getscore(9)) self.label.setStyleSheet("font:20pt '楷体';border-width: 1px;border-style: solid;border-color: rgb(255, 0, 0);") # 每过一秒发生的事情
def operate(self):
#具体操作
#print("计时结束") #地鼠全部藏起来
for i in range(1,10):
self.push_dic[i].setStyleSheet('QPushButton{background-image:url(tu.png)}')
self.flag = np.zeros(10) a=np.random.randint(1,10)
self.push_dic[a].setStyleSheet('QPushButton{background-image:url(mouse.png)}')
self.flag[a]=1
a=np.random.randint(1,10)
self.push_dic[a].setStyleSheet('QPushButton{background-image:url(mouse.png)}')
self.flag[a]=1
a=np.random.randint(1,10)
self.push_dic[a].setStyleSheet('QPushButton{background-image:url(mouse.png)}')
self.flag[a]=1 def game(self):
if self.begin == False:
self.end = False
self.begin = True
print("启动计时器")
self.timer.start(800) #设置计时间隔并启动
self.pushButton_10.setText("暂停") elif self.begin == True:
self.begin = False
self.timer.stop()
self.pushButton_10.setText("继续") def gameover(self):
if self.end == False:
self.begin = False
self.end == True
self.pushButton_10.setText("开始游戏")
self.flag = np.zeros(10)
self.timer.stop()
#地鼠全部藏起来
for i in range(1,10):
self.push_dic[i].setStyleSheet('QPushButton{background-image:url(tu.png)}')
self.score = 0
self.label.setText(str(self.score)) def getscore(self,a):
if self.flag[a] == 1 and self.begin == True:
self.score += 1
self.label.setText(str(self.score))
self.flag[a] = 0
self.push_dic[a].setStyleSheet('QPushButton{background-image:url(catch.png)}') app = QApplication(sys.argv)
form = MyWindow()
form.show()
app.exec_()

python programming作业11 Qt designer (打地鼠,不是很完美)的更多相关文章

  1. python programming作业10(仍有一点点小bug)

    # -*- coding: utf-8 -*- import os import platform import sys from PyQt5.QtCore import * from PyQt5.Q ...

  2. python programming作业5

      # -*- coding: utf-8 -*- class ageError(Exception): pass class salaryError(Exception): pass class s ...

  3. Qt Designer中自定义控件的使用(提升法与插件法)

    准备乱写一点Qt自定义Widget在Designer中的使用.可是又不想重复提升法(promotion)及插件法基本用法,因为Manual中Using Custom Widgets with Qt D ...

  4. 第15.11节 PyQt(Python+Qt)入门学习:Qt Designer(设计师)组件Property Editor(属性编辑)界面中主窗口QMainWindow类相关属性详解

    概述 主窗口对象是在新建窗口对象时,选择main window类型的模板时创建的窗口对象,如图: 在属性编辑界面中,主窗口对象与QMainWindow相关的属性包括:iconSize.toolButt ...

  5. python的部分GUI模块简介tkinter、pyqt5(Qt Designer)

    笔者认为,这两个作为Python3较为常用且简单的GUI模块,是Python开发者所必须学习至少是了解的. 其中tkinter为Python3自带的GUI模块,而pyqt5则需要通过pip insta ...

  6. PyQt(Python+Qt)学习随笔:Qt Designer中部件的autoFillBackground属性

    autoFillBackground属性可以确认部件背景是否自动填充,如果自动填充,Qt会在调用Paint事件之前填充部件的背景.使用的颜色由部件调色板中的QPalette.window 角色定义(关 ...

  7. PyQt(Python+Qt)学习随笔:Qt Designer中部件的样式表styleSheet属性

    styleSheet属性是定义部件外观的属性样式表,在Qt中styleSheet样式表是类似于html的css样式一样的方法,只是时专门为Qt中的部件开发的.styleSheet的定义语法也是类似CS ...

  8. PyQt(Python+Qt)学习随笔:Qt Designer中部件的layoutDirection属性

    layoutDirection属性保存的是部件的布局方向,有三个取值: 在部件上设置布局方向时,它将传播到部件的子级,但不会传播到作为窗口的子级,也不会传播到已显式调用setLayoutDirecti ...

  9. PyQt(Python+Qt)学习随笔:Qt Designer中部件的accessibleDescription和accessibleName辅助阅读属性

    accessibleDescription和accessibleName属性都是用于残疾人辅助阅读的,这两个属性都有国际化属性(关于国际化请参考<PyQt(Python+Qt)学习随笔:Qt D ...

随机推荐

  1. QTP(11)

    练习:录制Flight登录-打开1-10之间随机编号的订单-退出,打开完订单后,退出前,使用msgbox输出“顾客x预定了y从z到w的c类型的k张票!”说明:x是顾客姓名.y是日期.z是FlyFrom ...

  2. 牛客小白月赛12 H 华华和月月种树 (离线dfs序+线段树)

    链接:https://ac.nowcoder.com/acm/contest/392/H 来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 131072K,其他语言2621 ...

  3. Git使用教程学习

    Git使用教程学习 在第十二周的个人作业上,王文娟老师希望我们去自己课后了解一下git的使用方式以及一些基础知识,在本学期其他的课程上,我们已经稍微了解过一些git的基础知识,因此在本次作业里,我补充 ...

  4. CSS基础学习-15.CSS3 动画效果

  5. CSS基础学习-10.CSS伸缩盒(老版本)

  6. 理论基础+实战控制台程序实现AutoFac注入

    [半小时大话.net依赖注入](一)理论基础+实战控制台程序实现AutoFac注入   系列目录# 第一章|理论基础+实战控制台程序实现AutoFac注入 第二章|AutoFac的常见使用套路 第三章 ...

  7. bom操作,事件与jquery

    BOM操作中 window关键字 可以不写 DOM操作 学习如何查找节点 如何查找标签 选择器 事件 当符合某个条件下 自动触发的动作/响应 js绑定事件的方式 方式1 不推荐使用 <butto ...

  8. 解决git提交敏感信息(回退git版本库到某一个commit)

    解决git提交敏感信息(回退git版本库到某一个commit) Fri 07 June 2013 git是一个很好的版本库, 现在很多人用它, 并在github上创建项目, 相信大家都有过将敏感信息提 ...

  9. 使用fiddler抓取jmeter发送的请求

    使用jmeter发送请求时,有时需要查看发送的请求是否合理,可以使用fiddler更直观的抓取并查看jmeter发送的请求.步骤如下:1.设置fidder-connections 端口号为8888 2 ...

  10. 【Python之路】特别篇--五句话搞定JavaScript作用域

    JavaScript的作用域一直以来是前端开发中比较难以理解的知识点,对于JavaScript的作用域主要记住几句话,走遍天下都不怕... 一.“JavaScript中无块级作用域” 在Java或C# ...