일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- python
- pyqt
- JDBC
- 컬렉션프레임워크
- API
- 맥
- Java
- Error
- Homebrew
- Mac
- 생활코딩
- 이클립스
- Android
- jsp
- html
- spring
- 반복문
- 단축키
- nodejs
- crud
- 자바문제
- 자바
- ddit
- 객체지향
- 대덕인재개발원
- 배열
- FastAPI
- Oracle
- servlet
- ibatis
Archives
- Today
- Total
romworld
Python 13 - Pyqt(전화번호찍기, 곱하기 출력, 야구게임) 본문
전화번호찍기
<main08.ui>
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>331</width>
<height>312</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QPushButton" name="pb1">
<property name="geometry">
<rect>
<x>30</x>
<y>70</y>
<width>51</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>1</string>
</property>
</widget>
<widget class="QPushButton" name="pb2">
<property name="geometry">
<rect>
<x>90</x>
<y>70</y>
<width>51</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>2</string>
</property>
</widget>
<widget class="QPushButton" name="pb4">
<property name="geometry">
<rect>
<x>30</x>
<y>100</y>
<width>51</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>4</string>
</property>
</widget>
<widget class="QPushButton" name="pb3">
<property name="geometry">
<rect>
<x>150</x>
<y>70</y>
<width>51</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>3</string>
</property>
</widget>
<widget class="QPushButton" name="pb6">
<property name="geometry">
<rect>
<x>150</x>
<y>100</y>
<width>51</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>6</string>
</property>
</widget>
<widget class="QPushButton" name="pb5">
<property name="geometry">
<rect>
<x>90</x>
<y>100</y>
<width>51</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>5</string>
</property>
</widget>
<widget class="QPushButton" name="pb8">
<property name="geometry">
<rect>
<x>90</x>
<y>130</y>
<width>51</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>8</string>
</property>
</widget>
<widget class="QPushButton" name="pb7">
<property name="geometry">
<rect>
<x>30</x>
<y>130</y>
<width>51</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>7</string>
</property>
</widget>
<widget class="QPushButton" name="pb9">
<property name="geometry">
<rect>
<x>150</x>
<y>130</y>
<width>51</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>9</string>
</property>
</widget>
<widget class="QPushButton" name="pb0">
<property name="geometry">
<rect>
<x>30</x>
<y>160</y>
<width>51</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>0</string>
</property>
</widget>
<widget class="QPushButton" name="pbCall">
<property name="geometry">
<rect>
<x>90</x>
<y>160</y>
<width>111</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>CALL</string>
</property>
</widget>
<widget class="QLineEdit" name="le">
<property name="geometry">
<rect>
<x>30</x>
<y>40</y>
<width>171</width>
<height>20</height>
</rect>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>331</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
<main08.py>
import sys
from PyQt5.QtWidgets import *
from PyQt5 import uic
#.ui 파일 이름이 main01.ui 일때
form_class = uic.loadUiType("main08.ui")[0]
class MyWindow(QMainWindow, form_class):
def __init__(self):
super().__init__()
self.setupUi(self)
self.pb1.clicked.connect(self.myclick)
self.pb2.clicked.connect(self.myclick)
self.pb3.clicked.connect(self.myclick)
self.pb4.clicked.connect(self.myclick)
self.pb5.clicked.connect(self.myclick)
self.pb6.clicked.connect(self.myclick)
self.pb7.clicked.connect(self.myclick)
self.pb8.clicked.connect(self.myclick)
self.pb9.clicked.connect(self.myclick)
self.pb0.clicked.connect(self.myclick)
self.pbCall.clicked.connect(self.myCall)
def myCall(self):
str_tel = self.le.text()
QMessageBox.about(self,'calling',str_tel)
def myclick(self):
str_new = self.sender().text()
str_old = self.le.text()
self.le.setText(str_old+str_new)
##내용 추가
if __name__ == "__main__":
app = QApplication(sys.argv)
myWindow = MyWindow()
myWindow.show()
app.exec_()
1. android 의 toast(창 띄우기)는 python에서는 QMessageBox를 이용하면 된다.
2. sender()를 이용해 텍스트를 가져온다.
*결과
곱하기 출력
<main09.ui>
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>331</width>
<height>312</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QLineEdit" name="leA">
<property name="geometry">
<rect>
<x>30</x>
<y>60</y>
<width>51</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QLineEdit" name="leB">
<property name="geometry">
<rect>
<x>120</x>
<y>60</y>
<width>51</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QLineEdit" name="leC">
<property name="geometry">
<rect>
<x>230</x>
<y>60</y>
<width>41</width>
<height>20</height>
</rect>
</property>
</widget>
<widget class="QLabel" name="lbl">
<property name="geometry">
<rect>
<x>100</x>
<y>60</y>
<width>21</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>*</string>
</property>
</widget>
<widget class="QPushButton" name="pb">
<property name="geometry">
<rect>
<x>190</x>
<y>60</y>
<width>31</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>=</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>331</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
<main09.py>
1.첫번째 방법
import sys
from PyQt5.QtWidgets import *
from PyQt5 import uic
#.ui 파일 이름이 main01.ui 일때
form_class = uic.loadUiType("main09.ui")[0]
class MyWindow(QMainWindow, form_class):
def __init__(self):
super().__init__()
self.setupUi(self)
self.pb.clicked.connect(self.myclick)
def myclick(self):
a = self.leA.text()
aa = int(a)
b = self.leB.text()
bb = int(b)
self.leC.setText(str(self.multi(aa,bb)))
def multi(self,x,y):
mul = x * y
return mul
##내용 추가
if __name__ == "__main__":
app = QApplication(sys.argv)
myWindow = MyWindow()
myWindow.show()
app.exec_()
2. 두번째 방법
import sys
from PyQt5.QtWidgets import *
from PyQt5 import uic
#.ui 파일 이름이 main01.ui 일때
form_class = uic.loadUiType("main09.ui")[0]
class MyWindow(QMainWindow, form_class):
def __init__(self):
super().__init__()
self.setupUi(self)
self.pb.clicked.connect(self.myclick)
def myclick(self):
a = self.leA.text()
aa = int(a)
b = self.leB.text()
bb = int(b)
mul = aa * bb
self.leC.setText(str(mul))
##내용 추가
if __name__ == "__main__":
app = QApplication(sys.argv)
myWindow = MyWindow()
myWindow.show()
app.exec_()
결과
배수합
<main0a.ui>
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>331</width>
<height>312</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QLineEdit" name="leA">
<property name="geometry">
<rect>
<x>10</x>
<y>40</y>
<width>41</width>
<height>20</height>
</rect>
</property>
</widget>
<widget class="QLineEdit" name="leB">
<property name="geometry">
<rect>
<x>80</x>
<y>40</y>
<width>41</width>
<height>20</height>
</rect>
</property>
</widget>
<widget class="QLineEdit" name="leD">
<property name="geometry">
<rect>
<x>280</x>
<y>40</y>
<width>41</width>
<height>20</height>
</rect>
</property>
</widget>
<widget class="QLineEdit" name="leC">
<property name="geometry">
<rect>
<x>160</x>
<y>40</y>
<width>41</width>
<height>20</height>
</rect>
</property>
</widget>
<widget class="QLabel" name="lbl1">
<property name="geometry">
<rect>
<x>50</x>
<y>40</y>
<width>31</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>에서</string>
</property>
</widget>
<widget class="QLabel" name="lbl2">
<property name="geometry">
<rect>
<x>130</x>
<y>40</y>
<width>31</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>까지</string>
</property>
</widget>
<widget class="QPushButton" name="pb">
<property name="geometry">
<rect>
<x>210</x>
<y>40</y>
<width>61</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>배수합은</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>331</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
<main0a.py>
1. 첫번째 방법
import sys
from PyQt5.QtWidgets import *
from PyQt5 import uic
#.ui 파일 이름이 main01.ui 일때
form_class = uic.loadUiType("main0a.ui")[0]
class MyWindow(QMainWindow, form_class):
def __init__(self):
super().__init__()
self.setupUi(self)
self.pb.clicked.connect(self.myclick)
def myclick(self):
a = self.leA.text()
aa = int(a)
b = self.leB.text()
bb= int(b)
c = self.leC.text()
cc = int(c)
res = 0
for i in range(aa,bb+1):
if i % cc == 0:
res += i
self.leD.setText(str(res))
##내용 추가
if __name__ == "__main__":
app = QApplication(sys.argv)
myWindow = MyWindow()
myWindow.show()
app.exec_()
결과
야구게임
<main0b.ui>
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>331</width>
<height>312</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QLabel" name="lbl1">
<property name="geometry">
<rect>
<x>30</x>
<y>30</y>
<width>61</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>스트라이크</string>
</property>
</widget>
<widget class="QLineEdit" name="le">
<property name="geometry">
<rect>
<x>100</x>
<y>30</y>
<width>91</width>
<height>20</height>
</rect>
</property>
</widget>
<widget class="QPushButton" name="pb">
<property name="geometry">
<rect>
<x>30</x>
<y>60</y>
<width>161</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>맞추기</string>
</property>
</widget>
<widget class="QTextEdit" name="td">
<property name="geometry">
<rect>
<x>30</x>
<y>90</y>
<width>161</width>
<height>171</height>
</rect>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>331</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
<main0b.py>
import sys
from PyQt5.QtWidgets import *
from PyQt5 import uic
import random
#.ui 파일 이름이 main01.ui 일때
form_class = uic.loadUiType("main0b.ui")[0]
class MyWindow(QMainWindow, form_class):
def __init__(self):
super().__init__()
self.setupUi(self)
self.com = ""
self.pb.clicked.connect(self.myclick)
self.randomNum()
def randomNum(self):
arr = ["1","2","3","4","5","6","7","8","9"]
self.com = ""
for i in range(1,100+1):
rnd = int(random.random()*(len(arr)))
print(rnd)
a = arr[rnd]
b = arr[1]
arr[rnd] = b
arr[1] = a
self.com = arr[0] + arr[1] + arr[2]
print("self.com",self.com)
def myclick(self):
mine = self.le.text()
s = self.getStrike(self.com,mine)
b = self.getBall(self.com,mine)
str_old = self.td.toPlainText()
str_new = mine + " " + str(s) + "S" + str(b) + "B" +"\n"
self.td.setText(str_old+str_new)
self.le.setText("")
if s == 3:
QMessageBox.about(self, "야구게임", "정답입니다~")
# format방식으로 쓰는법
# QMessageBox.about(self, "야구게임","정답입니다~{}".format(self.com))
def getStrike(self,com,mine):
ret = 0
c1 = com[0:1]
c2 = com[1:2]
c3 = com[2:3]
m1 = mine[0:1]
m2 = mine[1:2]
m3 = mine[2:3]
if c1 == m1: ret += 1
if c2 == m2: ret += 1
if c3 == m3: ret += 1
return ret
def getBall(self,com,mine):
ret = 0
c1 = com[0:1]
c2 = com[1:2]
c3 = com[2:3]
m1 = mine[0:1]
m2 = mine[1:2]
m3 = mine[2:3]
if c1 == m2 or c1 == m3: ret += 1
if c2 == m1 or c2 == m3: ret += 1
if c3 == m1 or c3 == m2: ret += 1
return ret
##내용 추가
if __name__ == "__main__":
app = QApplication(sys.argv)
myWindow = MyWindow()
myWindow.show()
app.exec_()
1. 전역변수를 만들 때에도 (global이 있지만 ) 생성자 안에 선언해준다.
2. 다른 메서드에서 전역변수 선언시 꼭 self 잊지 말자
결과
'Python' 카테고리의 다른 글
Python 15 - SQLite (데이터 베이스 연동) / Anaconda.navigator (라이브러리 관리) (0) | 2023.01.04 |
---|---|
Python14 - Pymssql(MS SQL 연동) (0) | 2023.01.03 |
Python 12 - Pyqt ( 텍스트변경, 숫자 증가, 구구단, 홀짝, 로또게임, 가위바위보, 별찍기 (0) | 2022.12.30 |
Python11 - (Android), 숫자 더해서 출력, 배수합, 야구게임 (0) | 2022.12.29 |
Python10 - (Andoroid) Toast창 띄우기 (0) | 2022.12.29 |
Comments