Python's two GUI libraries (GTK and QT) HelloWorld

zhaozj2021-02-12  172

Some People Like Examples of The Code for a Widget Toolkit Before Downloading It. Here IS An Example of The Classic "Hello World" Program Written in Python with Pygtk:

Import gtk

DEF HELLO_CB (Button): Print "Hello World" Window.Destroy ()

window = gtk.Window (gtk.WINDOW_TOPLEVEL) # create a top level windowwindow.connect ( "destroy", gtk.mainquit) # quit the event loop on destructionwindow.set_border_width (10) # set padding round child widget

Button = gtk.button ("Hello World") Button.Connect ("Clicked", Hello_CB) # Call Hello_CB When ClickedWindow.Add (Button) # add button to windowbutton.show () # Show Button

WINDOW.SHOW () gtk.main () # ENTER THE Main Event Loop

This is the traditional "Hello World" Button Application, with as little code as possible: Hello Worldexample 6-1. Hello1.py - Hello World

## Hello1.py # import sys from qt import *

App = QApplication (sys.argv) Button = QPushbutton ("Hello World", None App.SetMainWidget (Button) Button.show () app.exec_loop ()

EXAMPLE 6-2. Hello2.py - a better Hello World

Import sysfrom qt import *

Class Hellobutton (QPushButton):

DEF __INIT __ (Self, * args): Apply (QPushButton .__ init__, (self) args) Self.Settext ("Hello World")

Class Hellovedow (QMainWindow):

DEF __INIT __ (Self, * args): Apply (QMainWindow .__ init__, (self) args) self.button = hellobutton (self) self.setcentralwidget (Self.Button)

Def main (args): app = qapplication (args) Win = hellowindow () win.show () app.connect (APP, Signal ("LastWindowClosed ()"), APP, SLOT ("Quit ()")) app. EXEC_LOOP () if __name __ == "__ main__": main (sys.argv)

转载请注明原文地址:https://www.9cbs.com/read-7176.html

New Post(0)