일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 31 |
Tags
- Error
- FastAPI
- Android
- JDBC
- 객체지향
- API
- html
- servlet
- jsp
- 대덕인재개발원
- 자바문제
- Oracle
- 이클립스
- 생활코딩
- Mac
- spring
- Homebrew
- 반복문
- 배열
- 컬렉션프레임워크
- 단축키
- 맥
- nodejs
- Java
- crud
- python
- ddit
- ibatis
- pyqt
- 자바
Archives
- Today
- Total
romworld
Python 12 - Pyqt ( 텍스트변경, 숫자 증가, 구구단, 홀짝, 로또게임, 가위바위보, 별찍기 본문
QT Design 의 ui 파일을 py으로 불러오기
(버튼을 눌렀을 때 라벨의 텍스트 변경)
<main01.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="lbl">
<property name="geometry">
<rect>
<x>60</x>
<y>70</y>
<width>131</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>Good Morning</string>
</property>
</widget>
<widget class="QPushButton" name="pb">
<property name="geometry">
<rect>
<x>170</x>
<y>70</y>
<width>75</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>click</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>
<main01.py>
import sys
from PyQt5.QtWidgets import *
from PyQt5 import uic
#.ui 파일 이름이 main01.ui 일때
form_class = uic.loadUiType("main01.ui")[0]
class MyWindow(QMainWindow, form_class):
def __init__(self):
super().__init__()
self.setupUi(self)
self.pb.clicked.connect(self.myclick)
def myclick(self):
self.lbl.setText("Good Evenig")
##내용 추가
if __name__ == "__main__":
app = QApplication(sys.argv)
myWindow = MyWindow()
myWindow.show()
app.exec_()
1. Designer에서 작성했던 버튼과 라벨의 변수명이 같아야한다.
실행시키면 윈도우창이 그대로 가져와진다.
숫자 증가시키기
<main02.py>
import sys
from PyQt5.QtWidgets import *
from PyQt5 import uic
from numpy import integer
#.ui 파일 이름이 main02.ui 일때
form_class = uic.loadUiType("main02.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.le.text()
aa = int(a)
aa += 1
self.le.setText(str(aa))
##내용 추가
if __name__ == "__main__":
app = QApplication(sys.argv)
myWindow = MyWindow()
myWindow.show()
app.exec_()
pyqt에서는 getText()가 아니라 text()를 써줘야지 텍스트가 가져와진다.
구구단 출력
[단수를 입력했을 때 그 단수에 맞는 구구단 출력]
<main03.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="lbl">
<property name="geometry">
<rect>
<x>100</x>
<y>60</y>
<width>61</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>출력단수 : </string>
</property>
</widget>
<widget class="QPushButton" name="pb">
<property name="geometry">
<rect>
<x>90</x>
<y>90</y>
<width>141</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>출력하기</string>
</property>
</widget>
<widget class="QLineEdit" name="le">
<property name="geometry">
<rect>
<x>160</x>
<y>60</y>
<width>61</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QTextEdit" name="te">
<property name="geometry">
<rect>
<x>100</x>
<y>120</y>
<width>131</width>
<height>141</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>
<main03.py>
1. 첫번째 방법 (내가 푼 것) for문
import sys
from PyQt5.QtWidgets import *
from PyQt5 import uic
from numpy import integer
#.ui 파일 이름이 main03.ui 일때
form_class = uic.loadUiType("main03.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.le.text()
aa = int(a)
res = ""
for i in range(1,9+1):
res += "{} x {} = {}".format(aa,i,aa*i) + "\n"
self.te.setText(res)
##내용 추가
if __name__ == "__main__":
app = QApplication(sys.argv)
myWindow = MyWindow()
myWindow.show()
app.exec_()
결과
홀짝게임
<main04.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>40</x>
<y>30</y>
<width>71</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>나 :</string>
</property>
</widget>
<widget class="QPushButton" name="pb">
<property name="geometry">
<rect>
<x>54</x>
<y>140</y>
<width>161</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>게임하기</string>
</property>
</widget>
<widget class="QLabel" name="lb2">
<property name="geometry">
<rect>
<x>40</x>
<y>60</y>
<width>71</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>컴 :</string>
</property>
</widget>
<widget class="QLabel" name="lbl3">
<property name="geometry">
<rect>
<x>40</x>
<y>90</y>
<width>71</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>결과 :</string>
</property>
</widget>
<widget class="QLineEdit" name="leMine">
<property name="geometry">
<rect>
<x>150</x>
<y>30</y>
<width>71</width>
<height>20</height>
</rect>
</property>
</widget>
<widget class="QLineEdit" name="leCom">
<property name="geometry">
<rect>
<x>150</x>
<y>60</y>
<width>71</width>
<height>20</height>
</rect>
</property>
</widget>
<widget class="QLineEdit" name="leResult">
<property name="geometry">
<rect>
<x>150</x>
<y>90</y>
<width>71</width>
<height>20</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>
<main04.py>
1. 첫번째 방법
import sys
from PyQt5.QtWidgets import *
from PyQt5 import uic
from numpy import integer
import random
#.ui 파일 이름이 main01.ui 일때
form_class = uic.loadUiType("main04.ui")[0]
class MyWindow(QMainWindow, form_class):
def __init__(self):
super().__init__()
self.setupUi(self)
self.pb.clicked.connect(self.myclick)
def myclick(self):
me = self.leMine.text()
com = ""
leResult = ""
rnd = random.random()
if rnd > 0.5 :
com = "홀"
else :
com= "짝"
if com == me :
leResult = "이김"
else :
leResult = "짐"
self.leCom.setText(com)
self.leResult.setText(leResult)
##내용 추가
if __name__ == "__main__":
app = QApplication(sys.argv)
myWindow = MyWindow()
myWindow.show()
app.exec_()
결과
로또게임
<main05.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>50</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>50</x>
<y>90</y>
<width>191</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>로또생성하기</string>
</property>
</widget>
<widget class="QLabel" name="lbl2">
<property name="geometry">
<rect>
<x>70</x>
<y>50</y>
<width>31</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>__</string>
</property>
</widget>
<widget class="QLabel" name="lbl3">
<property name="geometry">
<rect>
<x>110</x>
<y>50</y>
<width>31</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>__</string>
</property>
</widget>
<widget class="QLabel" name="lbl4">
<property name="geometry">
<rect>
<x>150</x>
<y>50</y>
<width>31</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>__</string>
</property>
</widget>
<widget class="QLabel" name="lbl5">
<property name="geometry">
<rect>
<x>200</x>
<y>50</y>
<width>31</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>__</string>
</property>
</widget>
<widget class="QLabel" name="lbl6">
<property name="geometry">
<rect>
<x>240</x>
<y>50</y>
<width>31</width>
<height>16</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>
<main05.py>
import sys
from PyQt5.QtWidgets import *
from PyQt5 import uic
from numpy import integer
import random
import math
#.ui 파일 이름이 main01.ui 일때
form_class = uic.loadUiType("main05.ui")[0]
class MyWindow(QMainWindow, form_class):
def __init__(self):
super().__init__()
self.setupUi(self)
self.pb.clicked.connect(self.myclick)
def myclick(self):
arr = [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,
31,32,33,34,35,36,37,
38,39,40,41,42,43,44,45
]
for i in range(1,100+1):
rnd = int(random.random()*len(arr))
a = arr[rnd]
b = arr[0]
arr[0] = a
arr[rnd] = b
self.lbl1.setText(str(arr[0]))
self.lbl2.setText(str(arr[1]))
self.lbl3.setText(str(arr[2]))
self.lbl4.setText(str(arr[3]))
self.lbl5.setText(str(arr[4]))
self.lbl6.setText(str(arr[5]))
##내용 추가
if __name__ == "__main__":
app = QApplication(sys.argv)
myWindow = MyWindow()
myWindow.show()
app.exec_()
결과
가위바위보
<main06.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>307</width>
<height>281</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>40</x>
<y>30</y>
<width>71</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>나 :</string>
</property>
</widget>
<widget class="QPushButton" name="pb">
<property name="geometry">
<rect>
<x>54</x>
<y>140</y>
<width>161</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>게임하기</string>
</property>
</widget>
<widget class="QLabel" name="lb2">
<property name="geometry">
<rect>
<x>40</x>
<y>60</y>
<width>71</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>컴 :</string>
</property>
</widget>
<widget class="QLabel" name="lbl3">
<property name="geometry">
<rect>
<x>40</x>
<y>90</y>
<width>71</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>결과 :</string>
</property>
</widget>
<widget class="QLineEdit" name="le_Mine">
<property name="geometry">
<rect>
<x>150</x>
<y>30</y>
<width>71</width>
<height>20</height>
</rect>
</property>
</widget>
<widget class="QLineEdit" name="le_Com">
<property name="geometry">
<rect>
<x>150</x>
<y>60</y>
<width>71</width>
<height>20</height>
</rect>
</property>
</widget>
<widget class="QLineEdit" name="le_Result">
<property name="geometry">
<rect>
<x>150</x>
<y>90</y>
<width>71</width>
<height>20</height>
</rect>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>307</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
<main06.py>
import sys
from PyQt5.QtWidgets import *
from PyQt5 import uic
from numpy import integer
import random
import math
#.ui 파일 이름이 main01.ui 일때
form_class = uic.loadUiType("main06.ui")[0]
class MyWindow(QMainWindow, form_class):
def __init__(self):
super().__init__()
self.setupUi(self)
self.pb.clicked.connect(self.myclick)
self.le_Mine.returnPressed.connect(self.myclick)
def myclick(self):
com = ""
mine = self.le_Mine.text()
result = ""
rnd = random.random()
if rnd > 0.66:
com ="가위"
elif rnd >0.33:
com = "바위"
else :
com = "보"
if com == "가위" and mine == "가위" : result = "비김"
if com == "가위" and mine == "바위" : result = "이김"
if com == "가위" and mine == "보" : result = "짐"
if com == "바위" and mine == "가위" : result = "짐"
if com == "바위" and mine == "바위" : result = "비김"
if com == "바위" and mine == "보" : result = "이김"
if com == "보" and mine == "가위" : result = "이김"
if com == "보" and mine == "바위" : result = "짐"
if com == "보" and mine == "보" : result = "비김"
self.le_Com.setText(com)
self.le_Result.setText(result)
##내용 추가
if __name__ == "__main__":
app = QApplication(sys.argv)
myWindow = MyWindow()
myWindow.show()
app.exec_()
__init__ : 생성자 , 보통 이벤트는 생성자에 많이 걸어준다
self.le_Mine.returnPressed.connect(self.myclick) : 내가 설정해준 le_Mine에서 엔터를 눌렀을 때 가위바위보 게임이 계속 진행되게 이벤트를 주는 것!
결과
별찍기
<main07.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>307</width>
<height>364</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>50</x>
<y>20</y>
<width>61</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>첫번째별수</string>
</property>
</widget>
<widget class="QPushButton" name="pb">
<property name="geometry">
<rect>
<x>40</x>
<y>90</y>
<width>191</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>출력하기</string>
</property>
</widget>
<widget class="QLabel" name="lbl2">
<property name="geometry">
<rect>
<x>50</x>
<y>50</y>
<width>61</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>마지막별수</string>
</property>
</widget>
<widget class="QLineEdit" name="le_first">
<property name="geometry">
<rect>
<x>140</x>
<y>20</y>
<width>71</width>
<height>20</height>
</rect>
</property>
</widget>
<widget class="QLineEdit" name="le_last">
<property name="geometry">
<rect>
<x>140</x>
<y>50</y>
<width>71</width>
<height>20</height>
</rect>
</property>
</widget>
<widget class="QPlainTextEdit" name="pte">
<property name="geometry">
<rect>
<x>50</x>
<y>120</y>
<width>181</width>
<height>201</height>
</rect>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>307</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
<main07.py>
import sys
from PyQt5.QtWidgets import *
from PyQt5 import uic
#.ui 파일 이름이 main01.ui 일때
form_class = uic.loadUiType("main07.ui")[0]
class MyWindow(QMainWindow, form_class):
def __init__(self):
super().__init__()
self.setupUi(self)
self.pb.clicked.connect(self.myclick)
def drawStar(self,cnt):
ret = ""
for i in range(cnt):
ret += "*"
ret += "\n"
return ret
def myclick(self):
f = self.le_first.text()
l = self.le_last.text()
ff = int(f)
ll = int(l)
txt = ""
for i in range(ff,ll+1):
txt += self.drawStar(i)
self.pte.setPlainText(txt)
##내용 추가
if __name__ == "__main__":
app = QApplication(sys.argv)
myWindow = MyWindow()
myWindow.show()
app.exec_()
결과
'Python' 카테고리의 다른 글
Python14 - Pymssql(MS SQL 연동) (0) | 2023.01.03 |
---|---|
Python 13 - Pyqt(전화번호찍기, 곱하기 출력, 야구게임) (1) | 2023.01.02 |
Python11 - (Android), 숫자 더해서 출력, 배수합, 야구게임 (0) | 2022.12.29 |
Python10 - (Andoroid) Toast창 띄우기 (0) | 2022.12.29 |
Python 09 - (Android)로또게임, 가위바위보, 별찍기 (0) | 2022.12.28 |
Comments