관리 메뉴

거니의 velog

231101_파이썬 기초 5 본문

대덕인재개발원_파이썬 프로그래밍

231101_파이썬 기초 5

Unlimited00 2023. 11. 1. 08:38

[MySwing05.java]

package day04;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class MySwing05 extends JFrame {

	private JPanel contentPane;
	private JTextField tf_mine;
	private JTextField tf_com;
	private JTextField tf_result;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MySwing05 frame = new MySwing05();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MySwing05() {
		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);
		
		JLabel lbl_mine = new JLabel("나:");
		lbl_mine.setBounds(64, 44, 57, 15);
		contentPane.add(lbl_mine);
		
		JLabel lbl_com = new JLabel("컴:");
		lbl_com.setBounds(64, 82, 57, 15);
		contentPane.add(lbl_com);
		
		JLabel lbl_result = new JLabel("결과:");
		lbl_result.setBounds(64, 122, 57, 15);
		contentPane.add(lbl_result);
		
		tf_mine = new JTextField();
		tf_mine.setBounds(177, 41, 116, 21);
		contentPane.add(tf_mine);
		tf_mine.setColumns(10);
		
		tf_com = new JTextField();
		tf_com.setColumns(10);
		tf_com.setBounds(177, 79, 116, 21);
		contentPane.add(tf_com);
		
		tf_result = new JTextField();
		tf_result.setColumns(10);
		tf_result.setBounds(177, 119, 116, 21);
		contentPane.add(tf_result);
		
		JButton btn = new JButton("게임하기");
		btn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				holJJack();
			}
		});
		btn.setBounds(64, 172, 97, 23);
		contentPane.add(btn);
	}
	
	void holJJack() {
		String mine = tf_mine.getText();
		String com = "";
		String result = "";
		
		double rnd = Math.random();
//		System.out.println(rnd);
		
		if(rnd > 0.5) {
			com = "홀";
		}else {
			com = "짝";
		}
		
		if(com.equals(mine)) {
			result = "이김";
		}else {
			result = "짐";
		}
		
		tf_com.setText(com);
		tf_result.setText(result);
	}
}

[선생님 풀이]

package day04;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class MySwing05 extends JFrame {

	private JPanel contentPane;
	private JTextField tf_mine;
	private JTextField tf_com;
	private JTextField tf_result;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MySwing05 frame = new MySwing05();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MySwing05() {
		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);
		
		JLabel lbl_mine = new JLabel("나:");
		lbl_mine.setBounds(64, 44, 57, 15);
		contentPane.add(lbl_mine);
		
		JLabel lbl_com = new JLabel("컴:");
		lbl_com.setBounds(64, 82, 57, 15);
		contentPane.add(lbl_com);
		
		JLabel lbl_result = new JLabel("결과:");
		lbl_result.setBounds(64, 122, 57, 15);
		contentPane.add(lbl_result);
		
		tf_mine = new JTextField();
		tf_mine.setBounds(177, 41, 116, 21);
		contentPane.add(tf_mine);
		tf_mine.setColumns(10);
		
		tf_com = new JTextField();
		tf_com.setColumns(10);
		tf_com.setBounds(177, 79, 116, 21);
		contentPane.add(tf_com);
		
		tf_result = new JTextField();
		tf_result.setColumns(10);
		tf_result.setBounds(177, 119, 116, 21);
		contentPane.add(tf_result);
		
		JButton btn = new JButton("게임하기");
		btn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				myclick();
			}
		});
		btn.setBounds(64, 172, 97, 23);
		contentPane.add(btn);
	}
	
	void myclick() {
		String mine = tf_mine.getText();
		String com = "";
		String result = "";
		
		double rnd = Math.random();
		
		if(rnd > 0.5) {
			com = "홀";
		}else {
			com = "짝";
		}
		
		if(mine.equals(com)) {
			result = "이김";
		}else {
			result = "짐";
		}
		
		tf_com.setText(com);
		tf_result.setText(result);
	}
}

[pyqt02.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>451</width>
    <height>250</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QLineEdit" name="le">
    <property name="geometry">
     <rect>
      <x>40</x>
      <y>30</y>
      <width>101</width>
      <height>20</height>
     </rect>
    </property>
    <property name="text">
     <string>10</string>
    </property>
    <property name="alignment">
     <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
    </property>
   </widget>
   <widget class="QPushButton" name="pb">
    <property name="geometry">
     <rect>
      <x>200</x>
      <y>30</y>
      <width>75</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>Double</string>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>451</width>
     <height>21</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

https://doc.qt.io/qt-6/qlineedit.html

 

QLineEdit Class | Qt Widgets 6.6.0

 

doc.qt.io

[day04.pyqt02]

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

form_class = uic.loadUiType("pyqt02.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("myclick")
        getTxt = self.le.text()
        # print(a)
        iTxt = int(getTxt)
        iTxt *= 2
        rtnTxt = str(iTxt)
        self.le.setText(rtnTxt) # <widget class="QLineEdit" name="le">
        
if __name__ == "__main__" :
    app = QApplication(sys.argv) 
    window = MainClass() 
    app.exec_()


[MySwing06.java]

package day04;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTextArea;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

public class MySwing06 extends JFrame {

	private JPanel contentPane;
	private JTextField tf;
	private JTextArea ta;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MySwing06 frame = new MySwing06();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MySwing06() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 359, 508);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lbl = new JLabel("출력단수:");
		lbl.setBounds(57, 41, 69, 15);
		contentPane.add(lbl);
		
		tf = new JTextField();
		tf.setBounds(163, 38, 116, 21);
		contentPane.add(tf);
		tf.setColumns(10);
		
		JButton btn = new JButton("출력하기");
		btn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				gugudan();
			}
		});
		btn.setBounds(47, 73, 232, 23);
		contentPane.add(btn);
		
		ta = new JTextArea();
		ta.setBounds(57, 120, 217, 311);
		contentPane.add(ta);
	}
	
	void gugudan() {
		
		String tfVal = tf.getText();
		int itf = Integer.parseInt(tfVal);
		List<String> rtnList = new ArrayList<String>();
		
		for(int i = 1; i < 10; i++) {
			//System.out.println(itf + "*" + i + "=" + (itf*i));
			rtnList.add(itf + "*" + i + "=" + (itf*i));
		}
		
		ta.setText(rtnList.get(0) + "\n" 
				+ rtnList.get(1) + "\n" 
				+ rtnList.get(2) + "\n" 
				+ rtnList.get(3) + "\n" 
				+ rtnList.get(4) + "\n" 
				+ rtnList.get(5) + "\n" 
				+ rtnList.get(6) + "\n" 
				+ rtnList.get(7) + "\n" 
				+ rtnList.get(8));

	}
}

[선생님 풀이]

package day04;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTextArea;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class MySwing06 extends JFrame {

	private JPanel contentPane;
	private JTextField tf;
	private JTextArea ta;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MySwing06 frame = new MySwing06();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MySwing06() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 359, 508);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lbl = new JLabel("출력단수:");
		lbl.setBounds(57, 41, 69, 15);
		contentPane.add(lbl);
		
		tf = new JTextField();
		tf.setBounds(163, 38, 116, 21);
		contentPane.add(tf);
		tf.setColumns(10);
		
		JButton btn = new JButton("출력하기");
		btn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				myclick();
			}
		});
		btn.setBounds(47, 73, 232, 23);
		contentPane.add(btn);
		
		ta = new JTextArea();
		ta.setBounds(57, 120, 217, 311);
		contentPane.add(ta);
	}
	
	void myclick() {
		
		String dan = tf.getText();
		int idan = Integer.parseInt(dan);
		
		String txt = "";
		
//		txt += dan+"*1="+(idan*1)+"\n";
//		txt += dan+"*2="+(idan*2)+"\n";
		
		for(int i = 1; i <= 9; i++) {
			txt += dan+"*"+i+"="+(idan*i)+"\n";
		} 
		
		ta.setText(txt);

	}
}

[MySwing07.java]

package day04;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class MySwing07 extends JFrame {

	private JPanel contentPane;
	private JTextField tfMine;
	private JTextField tfCom;
	private JTextField tfResult;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MySwing07 frame = new MySwing07();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MySwing07() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 351, 306);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblMine = new JLabel("나:");
		lblMine.setBounds(62, 47, 57, 15);
		contentPane.add(lblMine);
		
		JLabel lblCom = new JLabel("컴:");
		lblCom.setBounds(62, 93, 57, 15);
		contentPane.add(lblCom);
		
		JLabel lblResult = new JLabel("결과:");
		lblResult.setBounds(62, 140, 57, 15);
		contentPane.add(lblResult);
		
		tfMine = new JTextField();
		tfMine.setBounds(151, 44, 116, 21);
		contentPane.add(tfMine);
		tfMine.setColumns(10);
		
		tfCom = new JTextField();
		tfCom.setColumns(10);
		tfCom.setBounds(151, 90, 116, 21);
		contentPane.add(tfCom);
		
		tfResult = new JTextField();
		tfResult.setColumns(10);
		tfResult.setBounds(151, 137, 116, 21);
		contentPane.add(tfResult);
		
		JButton btn = new JButton("게임하기");
		btn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				myclick();
			}
		});
		btn.setBounds(62, 187, 205, 23);
		contentPane.add(btn);
	}
	
	void myclick() {
		String mine = tfMine.getText();
		String com = "";
		String result = "";
		
		double rnd = Math.random();
		
		if(rnd > 0.66) {
			com = "가위";
		}else if(rnd > 0.33) {
			com = "바위";
		}else {
			com = "보";
		}
		
		if(mine.equals("가위")) {
			if(com.equals("가위")) {
				result = "비김";
			}else if(com.equals("바위")) {
				result = "짐";
			}else {
				result = "이김";
			}
		}else if(mine.equals("바위")) {
			if(com.equals("가위")) {
				result = "이김";
			}else if(com.equals("바위")) {
				result = "비김";
			}else {
				result = "짐";
			}
		}else { // mine = 보
			if(com.equals("가위")) {
				result = "짐";
			}else if(com.equals("바위")) {
				result = "이김";
			}else {
				result = "비김";
			}
		}
		
		tfCom.setText(com);
		tfResult.setText(result);
	}

}

[선생님 풀이]

package day04;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class MySwing07 extends JFrame {

	private JPanel contentPane;
	private JTextField tfMine;
	private JTextField tfCom;
	private JTextField tfResult;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MySwing07 frame = new MySwing07();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MySwing07() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 351, 306);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblMine = new JLabel("나:");
		lblMine.setBounds(62, 47, 57, 15);
		contentPane.add(lblMine);
		
		JLabel lblCom = new JLabel("컴:");
		lblCom.setBounds(62, 93, 57, 15);
		contentPane.add(lblCom);
		
		JLabel lblResult = new JLabel("결과:");
		lblResult.setBounds(62, 140, 57, 15);
		contentPane.add(lblResult);
		
		tfMine = new JTextField();
		tfMine.setBounds(151, 44, 116, 21);
		contentPane.add(tfMine);
		tfMine.setColumns(10);
		
		tfCom = new JTextField();
		tfCom.setColumns(10);
		tfCom.setBounds(151, 90, 116, 21);
		contentPane.add(tfCom);
		
		tfResult = new JTextField();
		tfResult.setColumns(10);
		tfResult.setBounds(151, 137, 116, 21);
		contentPane.add(tfResult);
		
		JButton btn = new JButton("게임하기");
		btn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				myclick();
			}
		});
		btn.setBounds(62, 187, 205, 23);
		contentPane.add(btn);
	}
	
	void myclick() {
		String mine = tfMine.getText();
		String com = "";
		String result = "";
		
		double rnd = Math.random();
		
		if(rnd > 0.66) {
			com = "가위";
		}else if(rnd > 0.33) {
			com = "바위";
		}else {
			com = "보";
		}
		
		if(com.equals("가위") && mine.equals("가위")) { result = "비김"; }
		if(com.equals("가위") && mine.equals("바위")) { result = "이김"; }
		if(com.equals("가위") && mine.equals("보")) { result = "짐"; }
		
		if(com.equals("바위") && mine.equals("가위")) { result = "짐"; }
		if(com.equals("바위") && mine.equals("바위")) { result = "비김"; }
		if(com.equals("바위") && mine.equals("보")) { result = "이김"; }
		
		if(com.equals("보") && mine.equals("가위")) { result = "이김"; }
		if(com.equals("보") && mine.equals("바위")) { result = "짐"; }
		if(com.equals("보") && mine.equals("보")) { result = "비김"; }
		
		tfCom.setText(com);
		tfResult.setText(result);
	}

}

[MySwing08.java]

package day04;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTextArea;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class MySwing08 extends JFrame {

	private JPanel contentPane;
	private JTextField tf_first;
	private JTextField tf_last;
	private JTextArea ta;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MySwing08 frame = new MySwing08();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MySwing08() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 315, 583);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lbl_first = new JLabel("첫 별 수:");
		lbl_first.setBounds(42, 36, 80, 15);
		contentPane.add(lbl_first);
		
		JLabel lbl_last = new JLabel("끝 별 수:");
		lbl_last.setBounds(42, 80, 80, 15);
		contentPane.add(lbl_last);
		
		tf_first = new JTextField();
		tf_first.setBounds(133, 33, 116, 21);
		contentPane.add(tf_first);
		tf_first.setColumns(10);
		
		tf_last = new JTextField();
		tf_last.setColumns(10);
		tf_last.setBounds(133, 77, 116, 21);
		contentPane.add(tf_last);
		
		JButton btn = new JButton("별그리기");
		btn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				myclick();
			}
		});
		btn.setBounds(42, 125, 207, 23);
		contentPane.add(btn);
		
		ta = new JTextArea();
		ta.setBounds(42, 181, 207, 318);
		contentPane.add(ta);
	}
	
	String getStar(int cnt) {
		String txt = "";
		for(int i = 0; i < cnt; i++) {
			txt += "*";
		}
		return txt;
	}
	
	void myclick() {
		
		String tf_first_txt = tf_first.getText();
		String tf_last_txt = tf_last.getText();
		
		int ifir = Integer.parseInt(tf_first_txt);
		int ilas = Integer.parseInt(tf_last_txt);
		
		String rtn = "";
		for(int i = ifir; i < ilas+1; i++) {
			//System.out.println(getStar(i));
			rtn += getStar(i) + "\n";
		}
		
//		System.out.println(rtn);
		
		ta.setText(rtn);
		
	}

}

[선생님 풀이]

package day04;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTextArea;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class MySwing08 extends JFrame {

	private JPanel contentPane;
	private JTextField tf_first;
	private JTextField tf_last;
	private JTextArea ta;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MySwing08 frame = new MySwing08();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MySwing08() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 315, 583);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lbl_first = new JLabel("첫 별 수:");
		lbl_first.setBounds(42, 36, 80, 15);
		contentPane.add(lbl_first);
		
		JLabel lbl_last = new JLabel("끝 별 수:");
		lbl_last.setBounds(42, 80, 80, 15);
		contentPane.add(lbl_last);
		
		tf_first = new JTextField();
		tf_first.setBounds(133, 33, 116, 21);
		contentPane.add(tf_first);
		tf_first.setColumns(10);
		
		tf_last = new JTextField();
		tf_last.setColumns(10);
		tf_last.setBounds(133, 77, 116, 21);
		contentPane.add(tf_last);
		
		JButton btn = new JButton("별그리기");
		btn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				myclick();
			}
		});
		btn.setBounds(42, 125, 207, 23);
		contentPane.add(btn);
		
		ta = new JTextArea();
		ta.setBounds(42, 181, 207, 318);
		contentPane.add(ta);
	}
	
	String getStar(int cnt) {
		String ret = "";
		for(int i = 0; i < cnt; i++) {
			ret += "★";
		}
		return ret;
	}
	
	void myclick() {
		
		String f = tf_first.getText();
		String l = tf_last.getText();
		
		int ff = Integer.parseInt(f);
		int ll = Integer.parseInt(l);
		
		String txt = "";
		
//		txt += getStar(1) + "\n";
//		txt += getStar(2) + "\n";
		
		for(int i = ff; i <= ll; i++) {
			txt += getStar(i) + "\n";
		}
		
		ta.setText(txt);
		
	}

}


[pyqt03.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>582</width>
    <height>229</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QTextEdit" name="te1">
    <property name="geometry">
     <rect>
      <x>30</x>
      <y>20</y>
      <width>101</width>
      <height>31</height>
     </rect>
    </property>
   </widget>
   <widget class="QTextEdit" name="te2">
    <property name="geometry">
     <rect>
      <x>190</x>
      <y>20</y>
      <width>101</width>
      <height>31</height>
     </rect>
    </property>
   </widget>
   <widget class="QTextEdit" name="te3">
    <property name="geometry">
     <rect>
      <x>400</x>
      <y>20</y>
      <width>101</width>
      <height>31</height>
     </rect>
    </property>
   </widget>
   <widget class="QLabel" name="lbl">
    <property name="geometry">
     <rect>
      <x>150</x>
      <y>30</y>
      <width>56</width>
      <height>12</height>
     </rect>
    </property>
    <property name="text">
     <string>-</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pb">
    <property name="geometry">
     <rect>
      <x>310</x>
      <y>20</y>
      <width>75</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>582</width>
     <height>21</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

[day04.pyqt03]

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

form_class = uic.loadUiType("pyqt03.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("myclick")
        getTxt1 = self.te1.toPlainText() # <widget class="QTextEdit" name="te1">
        print(getTxt1)
        getTxt2 = self.te2.toPlainText() # <widget class="QTextEdit" name="te1">
        print(getTxt2)
        iTxt1 = int(getTxt1)
        iTxt2 = int(getTxt2)
        rtnTxt = iTxt1 - iTxt2
        #print(rtnTxt)
        self.te3.setText(str(rtnTxt))
        
if __name__ == "__main__" :
    app = QApplication(sys.argv) 
    window = MainClass() 
    app.exec_()


[pyqt04.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>786</width>
    <height>212</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QPlainTextEdit" name="pte1">
    <property name="geometry">
     <rect>
      <x>30</x>
      <y>20</y>
      <width>101</width>
      <height>31</height>
     </rect>
    </property>
   </widget>
   <widget class="QPlainTextEdit" name="pte2">
    <property name="geometry">
     <rect>
      <x>160</x>
      <y>20</y>
      <width>101</width>
      <height>31</height>
     </rect>
    </property>
   </widget>
   <widget class="QPlainTextEdit" name="pte3">
    <property name="geometry">
     <rect>
      <x>280</x>
      <y>20</y>
      <width>101</width>
      <height>31</height>
     </rect>
    </property>
   </widget>
   <widget class="QPlainTextEdit" name="pte4">
    <property name="geometry">
     <rect>
      <x>400</x>
      <y>20</y>
      <width>101</width>
      <height>31</height>
     </rect>
    </property>
   </widget>
   <widget class="QPlainTextEdit" name="pte5">
    <property name="geometry">
     <rect>
      <x>520</x>
      <y>20</y>
      <width>101</width>
      <height>31</height>
     </rect>
    </property>
   </widget>
   <widget class="QPlainTextEdit" name="pte6">
    <property name="geometry">
     <rect>
      <x>640</x>
      <y>20</y>
      <width>101</width>
      <height>31</height>
     </rect>
    </property>
   </widget>
   <widget class="QPushButton" name="pb">
    <property name="geometry">
     <rect>
      <x>30</x>
      <y>80</y>
      <width>711</width>
      <height>51</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>786</width>
     <height>21</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

[day04.pyqt04]

import sys
from PyQt5 import uic
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.Qt import QLineEdit
from random import random

form_class = uic.loadUiType("pyqt04.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("myclick")
        arr = list(range(1, 45+1))
        for i in range(1000):
            rnd = int(random() * 45)
            temp = arr[0]
            arr[0] = arr[rnd]
            arr[rnd] = temp
        # print("행운의 로또 번호는? {}, {}, {}, {}, {}, {}".format(arr[0], arr[1], arr[2], arr[3], arr[4], arr[5]))
        self.pte1.setPlainText(str(arr[0]))
        self.pte2.setPlainText(str(arr[1]))
        self.pte3.setPlainText(str(arr[2]))
        self.pte4.setPlainText(str(arr[3]))
        self.pte5.setPlainText(str(arr[4]))
        self.pte6.setPlainText(str(arr[5]))
        
if __name__ == "__main__" :
    app = QApplication(sys.argv) 
    window = MainClass() 
    app.exec_()


[MySwing09.java]

package day04;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.JButton;

public class MySwing09 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 {
					MySwing09 frame = new MySwing09();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MySwing09() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 281, 258);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		tf = new JTextField();
		tf.setBounds(31, 24, 203, 21);
		contentPane.add(tf);
		tf.setColumns(10);
		tf.setHorizontalAlignment(SwingConstants.RIGHT);
		
		JButton btn1 = new JButton("1");
		btn1 = createNumberButton("1");
		btn1.setBounds(31, 55, 59, 23);
		contentPane.add(btn1);
		
		JButton btn2 = new JButton("2");
		btn2 = createNumberButton("2");
		btn2.setBounds(102, 55, 59, 23);
		contentPane.add(btn2);
		
		JButton btn3 = new JButton("3");
		btn3 = createNumberButton("3");
		btn3.setBounds(175, 55, 59, 23);
		contentPane.add(btn3);
		
		JButton btn4 = new JButton("4");
		btn4 = createNumberButton("4");
		btn4.setBounds(31, 91, 59, 23);
		contentPane.add(btn4);
		
		JButton btn5 = new JButton("5");
		btn5 = createNumberButton("5");
		btn5.setBounds(102, 91, 59, 23);
		contentPane.add(btn5);
		
		JButton btn6 = new JButton("6");
		btn6 = createNumberButton("6");
		btn6.setBounds(175, 91, 59, 23);
		contentPane.add(btn6);
		
		JButton btn7 = new JButton("7");
		btn7 = createNumberButton("7");
		btn7.setBounds(31, 124, 59, 23);
		contentPane.add(btn7);
		
		JButton btn8 = new JButton("8");
		btn8 = createNumberButton("8");
		btn8.setBounds(102, 124, 59, 23);
		contentPane.add(btn8);
		
		JButton btn9 = new JButton("9");
		btn9 = createNumberButton("9");
		btn9.setBounds(175, 124, 59, 23);
		contentPane.add(btn9);
		
		JButton btn_call = new JButton("☎");
		btn_call.setBounds(102, 157, 132, 23);
		contentPane.add(btn_call);
		
		JButton btn0 = new JButton("0");
		btn0 = createNumberButton("0");
		btn0.setBounds(31, 157, 59, 23);
		contentPane.add(btn0);
		
		btn_call.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String text = tf.getText(); // tf에 입력된 텍스트 가져오기
                String message = "calling\n" + text;
                JOptionPane.showMessageDialog(null, message, "알림", JOptionPane.INFORMATION_MESSAGE);
            }
        });
	}
	
	private JButton createNumberButton(final String number) {
        JButton button = new JButton(number);
        button.addActionListener(new ActionListener() {
        	@Override
        	public void actionPerformed(ActionEvent e) {
                // 번호 버튼이 클릭되면 해당 번호를 tf에 추가
                tf.setText(tf.getText() + number);
            }
        });
        return button;
    }

}

'대덕인재개발원_파이썬 프로그래밍' 카테고리의 다른 글

231103_파이썬 기초 7  (0) 2023.11.03
231102_파이썬 기초 6  (0) 2023.11.02
231031_파이썬 기초 4  (0) 2023.10.31
231030_파이썬 기초 3  (0) 2023.10.30
231027_파이썬 기초 2  (0) 2023.10.27