반응형

/*********************************************************************************************************

-- Title : [PyQt4] Python programming.net 따라하기

-- Reference : pythonprogramming.net, Contact: Harrison@pythonprogramming.net
-- Key word : python pyqt pyqt4 qt qt4 파이썬 프로그래밍

*********************************************************************************************************/

 


- - Python

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# -*- coding: utf-8 -*-
 
import sys
from PyQt4 import QtGui, QtCore
 
 
 
class Window(QtGui.QMainWindow):
    # --▶ Init 메소드 생성
    def __init__(self):
        super(Window, self).__init__()                          # use super
        self.setGeometry(600300600400)                    # staring points and size of window
        self.setWindowTitle("Python Programming")               # set title
        self.setWindowIcon(QtGui.QIcon("./img/pythonlogo.png")) # set window icon
        self.statusBar()                                        # set statusbar
 
        self.tLabelText = "Please try to take some action."
 
        # --▶▶ 메뉴바 설정
        mainBar = self.menuBar()
 
        tFileAction = QtGui.QAction(QtGui.QIcon("./img/open.png"),"&Open"self)    # define submenu
        tFileAction.triggered.connect(self.on_open_file)
        tSaveAction = QtGui.QAction(QtGui.QIcon("./img/save.png"),"&Save"self)
        tSaveAction.triggered.connect(self.on_save_file)
        tExitAction = QtGui.QAction("&Exit"self)
        tExitAction.setStatusTip("Leave The App")
        tExitAction.triggered.connect(QtCore.QCoreApplication.instance().quit)
        tEditorAction = QtGui.QAction(QtGui.QIcon("./img/editor.png"),"&Editor"self)
        tEditorAction.triggered.connect(self.on_open_editor)
        tImportAction = QtGui.QAction(QtGui.QIcon("./img/import.png"),"&Import"self)
        tExportAction = QtGui.QAction(QtGui.QIcon("./img/export.png"),"&Export"self)
 
        tFileMenu = mainBar.addMenu("&File")               # add file menu
        tUtilMenu = mainBar.addMenu("&Util")               # add util menu
 
        tFileMenu.addAction(tFileAction)                   # add submenu under file menu
        tFileMenu.addAction(tSaveAction)
        tFileMenu.addSeparator()
        tFileMenu.addAction(tExitAction)
 
        tUtilMenu.addAction(tEditorAction)                 # add submenu under util menu
        tDataMenu = tUtilMenu.addMenu("&Data")
        tDataMenu.addAction(tImportAction)
        tDataMenu.addAction(tExportAction)
 
        # --▶▶ 일반 개체 생성 호출
        self.on_init_objects()
 
    # --▶ 일반 개체 설정
    def on_init_objects(self):
        self.on_button_quit()
        self.on_button_message()
 
        # --▶▶ 툴바 설정
        tFontAction = QtGui.QAction(QtGui.QIcon("./img/font.png"), "Change font color"self)
        tFontAction.triggered.connect(self.on_font_choice)
        tColorAction = QtGui.QAction(QtGui.QIcon("./img/color.png"), "Change bg font color"self)
        tColorAction.triggered.connect(self.on_color_picker)
        tSearchAction = QtGui.QAction(QtGui.QIcon("./img/msg.png"), "Open messagebox"self)
        tSearchAction.triggered.connect(self.on_print_msgbox)
        tDownAction = QtGui.QAction(QtGui.QIcon("./img/download.png"), "Download"self)
        tDownAction.triggered.connect(self.on_download)
        tNewAction = QtGui.QAction(QtGui.QIcon("./img/new.png"), "Open Window(widget)"self)
        tNewAction.triggered.connect(self.on_newopen_widget)
        tNew2Action = QtGui.QAction(QtGui.QIcon("./img/new.png"), "Open Window(dialog)"self)
        tNew2Action.triggered.connect(self.on_newopen_dialog)
 
        self.toolBar = self.addToolBar("Extraction")       # create toolbar object
        self.toolBar.addAction(tFontAction)
        self.toolBar.addAction(tColorAction)
        self.toolBar.addAction(tSearchAction)
        self.toolBar.addAction(tDownAction)
        self.toolBar.addAction(tNewAction)
        self.toolBar.addAction(tNew2Action)
 
        # --▶▶ 체크박스 설정
        tCheckbox = QtGui.QCheckBox("Enlarging"self)     # create checkbox object
        tCheckbox.move(20110)
        tCheckbox.stateChanged.connect(self.on_enlarge_window)
 
        # --▶▶ 프로그레스바 설정
        self.tProgress = QtGui.QProgressBar(self)          # create progressbar object
        self.tProgress.setGeometry(2131028020)
 
        # --▶▶ 라벨 설정
        self.tLabel = QtGui.QLabel(self)                   # create label object
        self.tLabel.setText(self.tLabelText)
        self.tLabel.setFrameShape(QtGui.QFrame.Panel)
        self.tLabel.resize(55020)
        self.tLabel.move(2165)
 
        tLabel2 = QtGui.QLabel(self)
        tLabel2.setText("Download:")
        tLabel2.move(22288)
 
        # --▶▶ 콥보박스 설정
        self.tCombobox = QtGui.QComboBox(self)             # craete combobox object
        self.tCombobox.addItem("motif")
        self.tCombobox.addItem("Windows")
        self.tCombobox.addItem("cde")
        self.tCombobox.addItem("Cleanlooks")
        self.tCombobox.addItem("windowsvista")
        self.tCombobox.move(2091)
        self.tCombobox.resize(10020)
 
        self.tCombobox.activated[str].connect(self.on_style_choice)
 
        # --▶▶ 달력 설정
        self.tCalendar = QtGui.QCalendarWidget(self)
        self.tCalendar.setGridVisible(True)
        self.tCalendar.move(31090)
        self.tCalendar.resize(260240)
        self.tCalendar.clicked.connect(self.on_choice_date)
 
    # --▶ 개체 Attribute 및 Signal 설정
    def on_newopen_dialog(self):
        self.tLabel.setText("Under construction...")
 
 
    def on_newopen_widget(self):                               # open new window
        self.tLabel.setText("Opened new Widget popup.")
        self.mPopup = NewPopup()
        self.mPopup.show()
 
    def on_save_file(self):
        tFileName = QtGui.QFileDialog.getSaveFileName(self'Save File')
        tFile = open(tFileName, 'w')
        tText = self.tTextedit.toPlainText()
        tFile.write(tText)
        tFile.close()
 
    def on_open_file(self):
        tFileName = QtGui.QFileDialog.getOpenFileName(self"Open File")
        tFile = open(tFileName, 'r')
        self.on_open_editor()
        with tFile:
            tText = tFile.read()
            self.tTextedit.setText(tText)
 
    def on_open_editor(self):
        self.tTextedit = QtGui.QTextEdit()
        self.setCentralWidget(self.tTextedit)
 
    def on_choice_date(self, date):
        self.tLabel.setText(str(date))
 
    def on_color_picker(self):                             # open color dialog
        tColor = QtGui.QColorDialog.getColor()
        self.tLabel.setStyleSheet("QWidget { background-color: %s}" % tColor.name())
 
        self.on_print_bgfont()
 
    def on_font_choice(self):                              # open font dialog widget
        font, valid = QtGui.QFontDialog.getFont()
        if valid:
            self.tLabel.setFont(font)
 
        self.on_print_font()
 
    def on_style_choice(self, text):                       # choose style
        self.tLabel.setText("You've chosen " + text + "! on combobox.")
        QtGui.QApplication.setStyle(QtGui.QStyleFactory.create(text))
 
    def on_button_quit(self):
        tButton = QtGui.QPushButton("Quit"self)          # create button object
        tButton.clicked.connect(QtCore.QCoreApplication.instance().quit)
        tButton.resize(tButton.minimumSizeHint())
        tButton.move(495350)
 
    def on_button_message(self):                           # create button object
        tButton = QtGui.QPushButton("Message"self)
        tButton.clicked.connect(self.on_print_button)
        tButton.resize(10022)
        tButton.move(130,90)
 
    def on_print_msgbox(self):                             # open messagebox
        result = QtGui.QMessageBox.question(self"Msgbox!""Under construction!", QtGui.QMessageBox.Yes | QtGui.QMessageBox.No)
        if result == QtGui.QMessageBox.Yes:
            self.tLabel.setText("You've chosen Yes! on Messagebox.")
        else:
            self.tLabel.setText("You've chosen No! on Messagebox.")
 
    def on_enlarge_window(self, state):                    # enlarge window size
        if state == QtCore.Qt.Checked:
            self.setGeometry(600300800600)
        else:
            self.setGeometry(600300600400)
 
        self.on_print_enlarge()
 
    def on_download(self):                                 # increase progress bar rate
        self.completed = 0
 
        while self.completed < 100:
            self.completed += 0.0001
            self.tProgress.setValue(self.completed)
 
        self.on_print_download()
 
    # --▶ 라벨 메시지 출력 정의
    def on_print_download(self):
        self.tLabel.setText("Download completed.")
 
    def on_print_enlarge(self):
        self.tLabel.setText("You clicked Enlarge Checkbox.")
 
    def on_print_font(self):
        self.tLabel.setText("You clicked Font Icon! on toolbar.")
 
    def on_print_bgfont(self):
        self.tLabel.setText("You clicked bgFont Icon! on toolbar.")
 
    def on_print_button(self):
        self.tLabel.setText("You clicked Message button!")
 
    def on_print_combobox(self):
        result1 = self.tCombobox.currentText()
        result2 = self.tCombobox.currentText()
 
        self.tLabel.setText("You've chosen combo list: " + result2 + "(" + str(result1) + " index)")
 
class NewPopup(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.setGeometry(100100400200)
 
        mTextbox = QtGui.QLineEdit(self)
        mTextbox.move(2020)
        mTextbox.resize(36040)
 
        mButton = QtGui.QPushButton("Close"self)
        mButton.move(20,80)
        mButton.clicked.connect(self.on_button_close)
 
    def on_button_close(self):
        self.close()
 
 
 
if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)  # define the app
    gui = Window()                      # define the gui
    gui.show()                          # show the gui
    sys.exit(app.exec_())               # run some simple exit code to ensure clean exit
 
 

cs



img.zip




반응형

+ Recent posts