Use the Tkinter module in Python to create a GUI program instance, pythontkinter

Source: Internet
Author: User

Use the Tkinter module in Python to create a GUI program instance, pythontkinter

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:
Copy codeThe Code is as follows:
# This program displays an empty window.
Import Tkinter
Def main ():
Main_window = Tkinter. Tk ()
Tkinter. mainloop ()
Main ()

Example 2:
Copy codeThe Code is as follows:
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:
Copy codeThe Code is as follows:
# 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:
Copy codeThe Code is as follows:
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:
Copy codeThe Code is as follows:
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:
Copy codeThe Code is as follows:
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:
Copy codeThe Code is as follows:
# 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:
Copy codeThe Code is as follows:
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:
Copy codeThe Code is as follows:
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:
Copy codeThe Code is as follows:
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:
Copy codeThe Code is as follows:
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.