본문 바로가기
Python/Python 기초

파이썬(QT) 별찍기 예제

by 미눅스[멘토] 2023. 6. 29.
728x90
import sys
from PyQt5 import(uic)
from PyQt5.QtWidgets import QApplication, QMainWindow
from random import random
from sympy.printing import str

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 myclick(self):
        f = self.le_first.text()
        l = self.le_last.text()
        ff = int(f)
        ll = int(l)
        
        str =""
        num = 0
        for i in range(ff,ll+1):
            for i in range(ff+num):
                str +="*"
                self.te.setText(str)
            num += 1    
            str+="\n"
        
        

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

결과

선생님 코드

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_()

 

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>80</x>
      <y>30</y>
      <width>61</width>
      <height>21</height>
     </rect>
    </property>
    <property name="text">
     <string>시작별수</string>
    </property>
   </widget>
   <widget class="QLineEdit" name="le_first">
    <property name="geometry">
     <rect>
      <x>210</x>
      <y>30</y>
      <width>113</width>
      <height>20</height>
     </rect>
    </property>
   </widget>
   <widget class="QLabel" name="lbl_last">
    <property name="geometry">
     <rect>
      <x>80</x>
      <y>70</y>
      <width>61</width>
      <height>21</height>
     </rect>
    </property>
    <property name="text">
     <string>끝별수</string>
    </property>
   </widget>
   <widget class="QLineEdit" name="le_last">
    <property name="geometry">
     <rect>
      <x>210</x>
      <y>80</y>
      <width>113</width>
      <height>20</height>
     </rect>
    </property>
   </widget>
   <widget class="QPushButton" name="pb">
    <property name="geometry">
     <rect>
      <x>70</x>
      <y>130</y>
      <width>211</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>190</y>
      <width>281</width>
      <height>291</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>