일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- EnhancedFor
- cursor문
- 인터페이스
- 한국건설관리시스템
- 참조형변수
- 사용자예외클래스생성
- 오라클
- 대덕인재개발원
- NestedFor
- 객체 비교
- abstract
- 집합_SET
- 예외미루기
- exception
- 생성자오버로드
- 환경설정
- 추상메서드
- 다형성
- 컬렉션프레임워크
- 정수형타입
- 어윈 사용법
- GRANT VIEW
- 자바
- 컬렉션 타입
- 제네릭
- 메소드오버로딩
- 예외처리
- 자동차수리시스템
- oracle
- Java
- Today
- Total
거니의 velog
231031_파이썬 기초 4 본문
[day03.mytest02]
# 2단에서 9단까지 출력하는 구구단 작성
for i in range(2, 9+1):
print("=== " + str(i) + "단 ===")
for j in range(1, 9+1):
print(str(i) + " * " + str(j) + " = " + str(i*j))
# 9단, 7단, 5단, 3단
for i in range(9, 1, -1):
if(i % 2 != 0):
print("=== " + str(i) + "단 ===")
for j in range(1, 9+1):
print(str(i) + " * " + str(j) + " = " + str(i*j))
# 9단, 8단, 2단
danList = [9, 8, 2]
for i in danList:
print("=== " + str(i) + "단 ===")
for j in range(1, 9+1):
print(str(i) + " * " + str(j) + " = " + str(i*j))
################################################
# 2단에서 9단까지 출력하는 구구단 작성
# def showDan(dan):
# for i in range(1, 9+1):
# print("{}*{}={}".format(dan,i,dan*i))
def showDan(dan):
print("{}*{}={}".format(dan,1,dan*1))
print("{}*{}={}".format(dan,2,dan*2))
print("{}*{}={}".format(dan,3,dan*3))
print("{}*{}={}".format(dan,4,dan*4))
# print("{}*{}={}".format(dan,5,dan*5))
print("{}*{}={}".format(dan,6,dan*6))
print("{}*{}={}".format(dan,7,dan*7))
print("{}*{}={}".format(dan,8,dan*8))
print("{}*{}={}".format(dan,9,dan*9))
for i in range(2, 9+1):
showDan(i)
# 9단, 7단, 5단, 3단
showDan(9)
showDan(7)
showDan(5)
showDan(3)
# 9단, 8단, 2단
showDan(9)
showDan(8)
showDan(2)
# 9단, 8단, 2단 중에서 5가 들어가면 삭제
showDan(9)
showDan(8)
showDan(2)
[GUI]
- Graphic User Interface
- Interface : 인간 <-> 컴퓨터 사이를 연결해 주는 모니터, 키보드, 마우스 등등
- UX : User Experience. 사용자 경험.
- TKinder, SimpleUI, PyQT ...
[ PyQT ] 우리는 이걸로 활용.
https://namu.wiki/w/Qt(%ED%94%84%EB%A0%88%EC%9E%84%EC%9B%8C%ED%81%AC)
https://ko.wikipedia.org/wiki/%EC%8A%A4%EC%9C%99_(%EC%9E%90%EB%B0%94)
https://m.blog.naver.com/khk6435/50179097524
https://ko.wikipedia.org/wiki/%EC%9E%90%EB%B0%94FX
[AwtTest.java]
package day04;
import java.awt.Frame;
public class AwtTest {
public static void main(String[] args) {
// Frame = window
Frame f = new Frame();
f.setVisible(true); // 창을 보이게 함
f.setSize(300, 500); // width, height
}
}
[SwingTest.java]
package day04;
import javax.swing.JFrame;
public class SwingTest {
public static void main(String[] args) {
JFrame f = new JFrame();
f.setVisible(true);
f.setSize(600, 300);
}
}
Event Driven Programming
- 언제 일어날지 모르는 상태가 이벤트.
- 갑작스레 발생하는 사건사고도 이벤트의 개념
- 상시 메모리에 상주하면서 켜져 있는 상태
https://eclipse.dev/windowbuilder/download.php
* 위지위그(WYSIWYG: What You See Is What You Get, "보는 대로 얻는다")
* 헝가리안 표기법, 모음이 빠지면서 글자가 짧아진다. c++, mfc에 많이 활용.
ex) button -> bttn, label -> lbl...
[MySwing01.java]
package day04;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JLabel;
public class MySwing01 extends JFrame {
private JPanel pan;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MySwing01 frame = new MySwing01();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public MySwing01() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
pan = new JPanel();
pan.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(pan);
pan.setLayout(null); // 절대 좌표계로 변환
JButton btn = new JButton("CLICK");
btn.setBounds(250, 64, 97, 23);
pan.add(btn);
JLabel lbl = new JLabel("Good Morning");
lbl.setBounds(86, 68, 106, 15);
pan.add(lbl);
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String evening = "Good Evening";
//System.out.println(evening);
lbl.setText(evening);
}
});
}
}
[선생님 풀이]
package day04;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class MySwing01 extends JFrame {
private JPanel pan;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MySwing01 frame = new MySwing01();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public MySwing01() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
pan = new JPanel();
pan.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(pan);
pan.setLayout(null); // 절대 좌표계로 변환
JLabel lbl = new JLabel("Good Morning");
lbl.setBounds(86, 68, 106, 15);
pan.add(lbl);
JButton btn = new JButton("CLICK");
btn.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
//System.out.println("MyClick");
lbl.setText("Good Evening");
}
});
btn.setBounds(250, 64, 97, 23);
pan.add(btn);
}
}
[MySwing02.java]
package day04;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class MySwing02 extends JFrame {
private JPanel contentPane;
private JTextField tf;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MySwing02 frame = new MySwing02();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public MySwing02() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
tf = new JTextField();
tf.setText("100");
tf.setBounds(42, 37, 116, 21);
contentPane.add(tf);
tf.setColumns(10);
JButton btn = new JButton("INCREASE");
btn.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
String strCnt = tf.getText();
int iCnt = Integer.parseInt(strCnt);
iCnt++;
String rtnCnt = String.valueOf(iCnt);
tf.setText(rtnCnt);
}
});
btn.setBounds(189, 36, 97, 23);
contentPane.add(btn);
}
}
[선생님 풀이]
package day04;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class MySwing02 extends JFrame {
private JPanel contentPane;
private JTextField tf;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MySwing02 frame = new MySwing02();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public MySwing02() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
tf = new JTextField();
tf.setText("100");
tf.setBounds(42, 37, 116, 21);
contentPane.add(tf);
tf.setColumns(10);
JButton btn = new JButton("INCREASE");
btn.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
myClick();
}
});
btn.setBounds(189, 36, 97, 23);
contentPane.add(btn);
}
void myClick() {
//System.out.println("myClick");
String strCnt = tf.getText();
int iCnt = Integer.parseInt(strCnt);
iCnt++;
//System.out.println("iCnt : " + iCnt);
tf.setText(iCnt + "");
}
}
[MySwing03.java]
package day04;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class MySwing03 extends JFrame {
private JPanel contentPane;
private JTextField tf1;
private JTextField tf2;
private JTextField tf3;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MySwing03 frame = new MySwing03();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public MySwing03() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
tf1 = new JTextField();
tf1.setBounds(23, 34, 70, 21);
contentPane.add(tf1);
tf1.setColumns(10);
tf2 = new JTextField();
tf2.setColumns(10);
tf2.setBounds(123, 34, 70, 21);
contentPane.add(tf2);
tf3 = new JTextField();
tf3.setColumns(10);
tf3.setBounds(282, 34, 70, 21);
contentPane.add(tf3);
JLabel lbl1 = new JLabel("+");
lbl1.setBounds(105, 37, 17, 15);
contentPane.add(lbl1);
JButton btn = new JButton("=");
btn.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
String val1 = tf1.getText();
String val2 = tf2.getText();
tf3.setText(add(val1, val2));
}
});
btn.setBounds(217, 34, 44, 21);
contentPane.add(btn);
}
String add(String val1, String val2) {
int ival1 = Integer.parseInt(val1);
int ival2 = Integer.parseInt(val2);
String rtnVal = String.valueOf(ival1 + ival2);
return rtnVal;
}
}
[선생님 풀이]
package day04;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class MySwing03 extends JFrame {
private JPanel contentPane;
private JTextField tf1;
private JTextField tf2;
private JTextField tf3;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MySwing03 frame = new MySwing03();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public MySwing03() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
tf1 = new JTextField();
tf1.setBounds(23, 34, 70, 21);
contentPane.add(tf1);
tf1.setColumns(10);
tf2 = new JTextField();
tf2.setColumns(10);
tf2.setBounds(123, 34, 70, 21);
contentPane.add(tf2);
tf3 = new JTextField();
tf3.setColumns(10);
tf3.setBounds(282, 34, 70, 21);
contentPane.add(tf3);
JLabel lbl1 = new JLabel("+");
lbl1.setBounds(105, 37, 17, 15);
contentPane.add(lbl1);
JButton btn = new JButton("=");
btn.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
myclick();
}
});
btn.setBounds(217, 34, 44, 21);
contentPane.add(btn);
}
void myclick() {
String a = tf1.getText();
String b = tf2.getText();
int aa = Integer.parseInt(a);
int bb = Integer.parseInt(b);
int sum = aa + bb;
System.out.println("sum : " + sum);
tf3.setText(sum + "");
}
}
[MySwing04.java]
package day04;
import java.awt.EventQueue;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class MySwing04 extends JFrame {
private JPanel contentPane;
JLabel lbl1 = new JLabel("__");
JLabel lbl2 = new JLabel("__");
JLabel lbl3 = new JLabel("__");
JLabel lbl4 = new JLabel("__");
JLabel lbl5 = new JLabel("__");
JLabel lbl6 = new JLabel("__");
JLabel[] labels = {lbl1, lbl2, lbl3, lbl4, lbl5, lbl6};
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MySwing04 frame = new MySwing04();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public MySwing04() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
lbl1.setBounds(32, 35, 26, 15);
contentPane.add(lbl1);
lbl2.setBounds(70, 35, 26, 15);
contentPane.add(lbl2);
lbl3.setBounds(108, 35, 26, 15);
contentPane.add(lbl3);
lbl4.setBounds(148, 35, 26, 15);
contentPane.add(lbl4);
lbl5.setBounds(189, 35, 26, 15);
contentPane.add(lbl5);
lbl6.setBounds(228, 35, 26, 15);
contentPane.add(lbl6);
JButton btn = new JButton(" 로또생성하기");
btn.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
lotto();
}
});
btn.setBounds(32, 76, 222, 28);
contentPane.add(btn);
}
public void lotto() {
ArrayList<Integer> lottoNum = new ArrayList<Integer>();
// 레이블 배열 생성
int i = 0;
while(i < 6) {
Integer temp = (int)(Math.random() * 45 + 1);
if(!(lottoNum.contains(temp))) {
System.out.println(temp);
lottoNum.add(temp);
labels[i].setText(temp+"");
i++;
}
}
}
}
[pyqt01.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>429</width>
<height>220</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>70</x>
<y>80</y>
<width>101</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>230</x>
<y>80</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>429</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
[day04.pyqt01]
# UI파일 연결
# UI파일 위치를 잘 적어 넣어준다.
import sys
from PyQt5 import uic
from PyQt5.QtWidgets import QApplication, QMainWindow
form_class = uic.loadUiType("./pyqt01.ui")[0]
# 프로그램 메인을 담당하는 Class 선언
class MainClass(QMainWindow, form_class):
def __init__(self) :
QMainWindow.__init__(self)
# 연결한 Ui를 준비한다.
self.setupUi(self)
# 화면을 보여준다.
self.show()
# 버튼에 클릭 이벤트 연결
self.pb.clicked.connect(self.mouseClicked)
def mouseClicked(self):
# 버튼이 클릭되었을 때 실행할 코드 작성
self.lbl.setText("Good Evening") # 예시: 라벨에 텍스트 설정
if __name__ == "__main__" :
app = QApplication(sys.argv)
window = MainClass()
app.exec_()
[선생님 풀이]
[MySwing04.java]
package day04;
import java.awt.EventQueue;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class MySwing04 extends JFrame {
private JPanel contentPane;
JLabel lbl1, lbl2, lbl3, lbl4, lbl5, lbl6;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MySwing04 frame = new MySwing04();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public MySwing04() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
lbl1 = new JLabel("__");
lbl1.setBounds(32, 35, 26, 15);
contentPane.add(lbl1);
lbl2 = new JLabel("__");
lbl2.setBounds(70, 35, 26, 15);
contentPane.add(lbl2);
lbl3 = new JLabel("__");
lbl3.setBounds(108, 35, 26, 15);
contentPane.add(lbl3);
lbl4 = new JLabel("__");
lbl4.setBounds(148, 35, 26, 15);
contentPane.add(lbl4);
lbl5 = new JLabel("__");
lbl5.setBounds(189, 35, 26, 15);
contentPane.add(lbl5);
lbl6 = new JLabel("__");
lbl6.setBounds(228, 35, 26, 15);
contentPane.add(lbl6);
JButton btn = new JButton("로또생성하기");
btn.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
lotto();
}
});
btn.setBounds(32, 76, 222, 28);
contentPane.add(btn);
}
public void lotto() {
int[] 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(int i = 0; i < 1000; i++) {
int rnd = (int) (Math.random() * 45);
int temp = arr[0];
arr[0] = arr[rnd];
arr[rnd] = temp;
}
for(int j = 0; j < arr.length; j++) {
System.out.print(arr[j] + ", ");
}
lbl1.setText(arr[0] + "");
lbl2.setText(arr[1] + "");
lbl3.setText(arr[2] + "");
lbl4.setText(arr[3] + "");
lbl5.setText(arr[4] + "");
lbl6.setText(arr[5] + "");
}
}
[day04.pyqt01]
import sys
from PyQt5 import uic
from PyQt5.QtWidgets import QApplication, QMainWindow
form_class = uic.loadUiType("pyqt01.ui")[0]
class MainClass(QMainWindow, form_class):
def __init__(self) :
QMainWindow.__init__(self)
self.setupUi(self)
self.pb.clicked.connect(self.myclick) # <widget class="QPushButton" name="pb">
self.show()
def myclick(self) :
# print("btn_1 Clicked")
self.lbl.setText("Good Evening") # <widget class="QLabel" name="lbl">
if __name__ == "__main__" :
app = QApplication(sys.argv)
window = MainClass()
app.exec_()
'대덕인재개발원 > 대덕인재개발원_파이썬 프로그래밍' 카테고리의 다른 글
231102_파이썬 기초 6 (0) | 2023.11.02 |
---|---|
231101_파이썬 기초 5 (0) | 2023.11.01 |
231030_파이썬 기초 3 (0) | 2023.10.30 |
231027_파이썬 기초 2 (0) | 2023.10.27 |
231026_파이썬 기초 1 (0) | 2023.10.26 |