Creating a GUI program instance using the Tkinter module in Python

Source: Internet
Author: User
Use the Tkinter module to create a simple GUI program.

Tkinter's widgets are: Button, Canvas, Checkbutton, Entry, Frame, Label, Listbox, Menu, Menubutton, Message, Radiobutton, Scales, Scrollbar, TEXT, TopLevel and so on.

Cases:

The code is as follows:


# This program displays an empty window.
Import Tkinter
def main ():
Main_window = tkinter.tk ()
Tkinter.mainloop ()
Main ()


Example 2:

The 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:

The 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:

The 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 was my GUI program. ')
Self.label1.pack ()
Self.label2.pack ()
Tkinter.mainloop ()
Mygui = Mygui ()


Example 5:

The 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 was my GUI program. ')
Self.label1.pack (side= ' left ')
Self.label2.pack (side= ' left ')
Tkinter.mainloop ()
Mygui = Mygui ()


Example 6:

The 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.
Cases:

The code is as follows:


# The program demonstrates a Button widget.
# When the user clicks the button, a 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:

The 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 ()


Get Input with entry widget
The Entry widget is a rectangular area that users can enter. The input data can be retrieved using the Get method of the entry widget.
Cases:

The 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:

The 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 button and check button
Cases:

The 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.RB1 = 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 ()

  • Related Article

    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.