Python GUI Programming

Source: Internet
Author: User

Python GUI Programming

Use the tkinter module to create a simple GUI program.
Tkinter's widgets include buttons, canvas, checkbutton, entry, frame, label, ListBox, menu, menubutton, message, radiobutton, scales, scrollbar, text, and toplevel.

Example:
# This program displays an empty window.
Import tkinter
Def main ():
Main_window = tkinter. TK ()
Tkinter. mainloop ()
Main ()

Example 2:
Import tkinter
Class mygui:
Def _ init _ (Self ):
# Create the Main Window widget.
Self. main_window = tkinter. TK ()

# Enter the tkinter main loop.
Tkinter. mainloop ()
# Create an instance of the mygui class.
My_gui = mygui ()

Example 3:
# The program displays a label with text.
Import tkinter
Class mygui:
Def _ init _ (Self ):
Self. main_window = tkinter. TK ()
# Create a label widget containing the text 'Hello world'
Self. Label = tkinter. Label (self. main_window, text = 'Hello world! ')
# Call the label widget's pack method.
Self. Label. Pack ()
# Enter the tkinter main loop.
Tkinter. mainloop ()
# Create an instance of the mygui class.
My_gui = mygui ()

Example 4:
Import tkinter
Class mygui:
Def _ init _ (Self ):
Self. main_window = tkinter. TK ()
Self. label1 = tkinter. Label (self. main_window, text = 'Hello world! ')
Self. label2 = tkinter. Label (self. main_window, text = 'This is my gui program .')
Self. label1.pack ()
Self. label2.pack ()
Tkinter. mainloop ()
Mygui = mygui ()

Example 5:
Import tkinter
Class mygui:
Def _ init _ (Self ):
Self. main_window = tkinter. TK ()
Self. label1 = tkinter. Label (self. main_window, text = 'Hello world! ')
Self. label2 = tkinter. Label (self. main_window, text = 'This is my gui program .')
Self. label1.pack (side = 'left ')
Self. label2.pack (side = 'left ')
Tkinter. mainloop ()
Mygui = mygui ()

Example 6:
Import tkinter
Class mygui:
Def _ init _ (Self ):
Self. main_window = tkinter. TK ()
Self. top_frame = tkinter. Frame (self. main_window)
Self. bottom_frame = tkinter. Frame (self. main_window)
Self. label1 = tkinter. Label (self. top_frame, text = 'winken ')
Self. label2 = tkinter. Label (self. top_frame, text = 'blinken ')
Self. label3 = tkinter. Label (self. top_frame, text = 'nod ')

Self. label1.pack (side = 'top ')
Self. label2.pack (side = 'top ')
Self. label3.pack (side = 'top ')

Self. label4 = tkinter. Label (self. bottom_frame, text = 'winken ')
Self. label5 = tkinter. Label (self. bottom_frame, text = 'blinken ')
Self. label6 = tkinter. Label (self. bottom_frame, text = 'nod ')

Self. label4.pack (side = 'left ')
Self. label5.pack (side = 'left ')
Self. label6.pack (side = 'left ')

Self. top_frame.pack ()
Self. bottom_frame.pack ()

Tkinter. mainloop ()
Mygui = mygui ()

Button widget and information dialog box
Use the showinfo function of the tkmessagebox module to display the information dialog box.
Example:
# The program demonstrates a Button widget.
# When the user clicks the button, an info dialog box is displayed.
Import tkinter
Import tkmessagebox
Class mygui:
Def _ init _ (Self ):
Self. main_window = tkinter. TK ()
Self. my_button = tkinter. Button (self. main_window, text = 'click me! ', Command = self. do_something)
Self. my_button.pack ()
Tkinter. mainloop ()
Def do_something (Self ):
Tkmessagebox. showinfo ('response', 'Thanks for clicking the button .')
Mygui = mygui ()

Example 2:
Import tkinter
Import tkmessagebox
Class mygui:
Def _ init _ (Self ):
Self. main_window = tkinter. TK ()
Self. my_button = tkinter. Button (self. main_window, text = 'click me! ', Command = self. do_something)
Self. quit_button = tkinter. Button (self. main_window, text = 'quit', command = self. main_window.quit)
Self. my_button.pack ()
Self. quit_button.pack ()
Tkinter. mainloop ()
Def do_something (Self ):
Tkmessagebox. showinfo ('response', 'Thanks for clicking the button .')
Mygui = mygui ()

Use the entry widget to obtain the input.
The entry widget is a rectangular area that you can enter. You can use the get method of the entry widget to retrieve the input data.
Example:
Import tkinter
Import tkmessagebox
Class kilogui:
Def _ init _ (Self ):
Self. main_window = tkinter. TK ()
Self. top_frame = tkinter. Frame (self. main_window)
Self. bottom_frame = tkinter. Frame (self. main_window)
Self. Label = tkinter. Label (self. top_frame, text = 'enter a distance in kilometers :')
Self. Entry = tkinter. Entry (self. top_frame, width = 10)
Self. button1 = tkinter. Button (self. bottom_frame, text = 'convert', command = self. Convert)
Self. button2 = tkinter. Button (self. bottom_frame, text = 'quit', command = self. main_window.quit)

Self. Label. Pack (side = 'left ')
Self. Entry. Pack (side = 'left ')
Self. button1.pack (side = 'left ')
Self. button2.pack (side = 'left ')
Self. top_frame.pack ()
Self. bottom_frame.pack ()

Tkinter. mainloop ()
Def convert (Self ):
Kilo = float (self. Entry. Get ())
Miles = kilo * 0.6214
Tkmessagebox. showinfo ('result', STR (kilo) + 'kilometers is equal to '+ STR (Miles) + 'miles .')

Mygui = kilogui ()

Example 2:
Import tkinter
Import tkmessagebox
Class kilogui:
Def _ init _ (Self ):
Self. main_window = tkinter. TK ()
Self. top_frame = tkinter. Frame (self. main_window)
Self. mid_frame = tkinter. Frame (self. main_window)
Self. bottom_frame = tkinter. Frame (self. main_window)

Self. label1 = tkinter. Label (self. top_frame, text = 'enter a distance in kilometers :')
Self. Entry = tkinter. Entry (self. top_frame, width = 10)

Self. button1 = tkinter. Button (self. bottom_frame, text = 'convert', command = self. Convert)
Self. button2 = tkinter. Button (self. bottom_frame, text = 'quit', command = self. main_window.quit)

Self. label2 = tkinter. Label (self. mid_frame, text = 'converted to Miles :')
Self. value = tkinter. stringvar ()
Self. label3 = tkinter. Label (self. mid_frame, textvariable = self. value)

Self. label1.pack (side = 'left ')
Self. Entry. Pack (side = 'left ')

Self. button1.pack (side = 'left ')
Self. button2.pack (side = 'left ')

Self. label2.pack (side = 'left ')
Self. label3.pack (side = 'left ')

Self. top_frame.pack ()
Self. mid_frame.pack ()
Self. bottom_frame.pack ()

Tkinter. mainloop ()
Def convert (Self ):
Kilo = float (self. Entry. Get ())
Miles = kilo * 0.6214
Self. value. Set (Miles)

Mygui = kilogui ()

Radio and check buttons
Example:
Import tkinter
Import tkmessagebox
Class mygui:
Def _ init _ (Self ):
Self. main_window = tkinter. TK ()
Self. top_frame = tkinter. Frame (self. main_window)
Self. bottom_frame = tkinter. Frame (self. main_window)

Self. radio_var = tkinter. intvar ()
Self. radio_var.set (1)
Self. rb= tkinter. radiobutton (self. top_frame, text = 'option 1', variable = self. radio_var, value = 1)
Self. Rb2 = tkinter. radiobutton (self. top_frame, text = 'option 2', variable = self. radio_var, value = 2)
Self. Rb3 = tkinter. radiobutton (self. top_frame, text = 'option 3', variable = self. radio_var, value = 3)

Self. rb1.pack ()
Self. rb2.pack ()
Self. rb3.pack ()

Self. OK _button = tkinter. Button (self. bottom_frame, text = 'OK', command = self. show_choice)
Self. quit_button = tkinter. Button (self. bottom_frame, text = 'quit', command = self. main_window.quit)

Self. OK _button.pack (side = 'left ')
Self. quit_button.pack (side = 'left ')

Self. top_frame.pack ()
Self. bottom_frame.pack ()

Tkinter. mainloop ()

Def show_choice (Self ):
Tkmessagebox. showinfo ('selection ', 'you selected optioin' + STR (self. radio_var.get ()))
Mygui = mygui ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.