본문 바로가기

Python

[QT] 별찍기

myqt07.py

import sys
from PyQt5 import uic
from PyQt5.QtWidgets import QApplication, QMainWindow

form_class = uic.loadUiType("myqt07.ui")[0]


class WindowClass(QMainWindow, form_class):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.pb.clicked.connect(self.btnClick)
        
    def btnClick(self):
        a = int(self.le_first.text())
        b = int(self.le_last.text())
        star = ""
        for i in range(a,b+1):
            for _ in range(i):
                star += "*"
            star += "\n"
        self.te.setText(star)
        
    # def getStar(self,cnt):
    #     ret = ""
    #     for i in range(cnt):
    #         ret += "*"
    #     ret += "\n"
    #     return ret


if __name__ == "__main__":
    app = QApplication(sys.argv)
    myWindow = WindowClass()
    myWindow.show()
    app.exec_()

별을 찍는 메서드를 만들어 for문으로 메서드를 실행해 주는 방법도 있다.

import sys
from PyQt5 import uic
from PyQt5.QtWidgets import QApplication, QMainWindow
from conda_env.specs import binstar


form_class = uic.loadUiType("./myqt07.ui")[0]


class WindowClass(QMainWindow, form_class):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.pb.clicked.connect(self.myclick)
        
    def getStar(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.getStar(i)
        
        self.te.setText(txt)



if __name__ == "__main__":
    app = QApplication(sys.argv)
    myWindow = WindowClass()
    myWindow.show()
    app.exec_()

 

myqt07.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>800</width>
    <height>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QLabel" name="lbl_first">
    <property name="geometry">
     <rect>
      <x>90</x>
      <y>70</y>
      <width>56</width>
      <height>12</height>
     </rect>
    </property>
    <property name="text">
     <string>시작 별 수</string>
    </property>
   </widget>
   <widget class="QLabel" name="lbl_last">
    <property name="geometry">
     <rect>
      <x>90</x>
      <y>120</y>
      <width>56</width>
      <height>12</height>
     </rect>
    </property>
    <property name="text">
     <string>끝 별 수</string>
    </property>
   </widget>
   <widget class="QLineEdit" name="le_first">
    <property name="geometry">
     <rect>
      <x>170</x>
      <y>60</y>
      <width>113</width>
      <height>20</height>
     </rect>
    </property>
   </widget>
   <widget class="QLineEdit" name="le_last">
    <property name="geometry">
     <rect>
      <x>170</x>
      <y>110</y>
      <width>113</width>
      <height>20</height>
     </rect>
    </property>
   </widget>
   <widget class="QPushButton" name="pb">
    <property name="geometry">
     <rect>
      <x>90</x>
      <y>150</y>
      <width>191</width>
      <height>41</height>
     </rect>
    </property>
    <property name="text">
     <string>별그리기</string>
    </property>
   </widget>
   <widget class="QTextEdit" name="te">
    <property name="geometry">
     <rect>
      <x>90</x>
      <y>210</y>
      <width>191</width>
      <height>311</height>
     </rect>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>800</width>
     <height>21</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

 

'Python' 카테고리의 다른 글

[QT] 오목 게임  (0) 2023.07.03
[QT] 야구 게임  (0) 2023.06.29
[QT] 가위바위보 게임  (0) 2023.06.29
[QT] 전화기 만들기  (0) 2023.06.28
[QT] 구구단 출력하기  (0) 2023.06.28