約2週間前にPythonを始めたばかりです。現在、Glade を使用して PyGObject で GUI を作成しようとしています。
しかし、プログラムの全体的なレイアウトがどうあるべきか、私は戸惑っています。
メインプログラムとシグナルにクラスを使用する必要がありますか、それともそれらを分離する必要がありますか?
これに対する「最善のアプローチ」はありますか?
または、以下の私の謙虚なアプローチのように、クラスをまったく使用しないでください。
以下の例の関数間で通信するにはどうすればよいですか? たとえば、関数のparent
パラメーターをGtk.MessageDialog
プログラムのメイン ウィンドウとして設定するにはどうすればよいですか?
Python コード:
#!/usr/bin/python
try:
from gi.repository import Gtk
except:
print('Cannot Import Gtk')
sys.exit(1)
# Confirm and exit when Quit button is clicked.
def on_button_quit_clicked(widget):
confirmation_dialog = Gtk.MessageDialog(parent = None,
flags = Gtk.DialogFlags.DESTROY_WITH_PARENT,
type = Gtk.MessageType.QUESTION,
buttons = Gtk.ButtonsType.YES_NO,
message_format =
'Are you sure you want to quit?')
print ('Quit confirmation dialog is running.')
confirmation_response = confirmation_dialog.run()
if confirmation_response == Gtk.ResponseType.YES:
print ('You have clicked on YES, quiting..')
Gtk.main_quit()
elif confirmation_response == Gtk.ResponseType.NO:
print ('You have clicked on NO')
confirmation_dialog.destroy()
print ('Quit confirmation dialog is destroyed.')
# Show About dialog when button is clicked.
def on_button_about_clicked(widget):
print ('About')
# Perform addition when button is clicked.
def on_button_add_clicked(widget):
print ('Add')
# Main function
def main():
builder = Gtk.Builder()
builder.add_from_file('CalculatorGUI.glade')
signalHandler = {
'on_main_window_destroy': Gtk.main_quit,
'on_button_quit_clicked': on_button_quit_clicked,
'on_button_about_clicked': on_button_about_clicked,
'on_button_add_clicked': on_button_add_clicked
}
builder.connect_signals(signalHandler)
main_window = builder.get_object('main_window')
main_window.show_all()
Gtk.main()
print ('Program Finished!')
# If the program is not imported as a module, then run.
if __name__ == '__main__':
main()
CalculatorGUI.glade
ファイルの成分: http://pastebin.com/K2wb7Z4r
プログラムのスクリーンショット: