Python Tkinter basic control usage, pythontkinter

Source: Internet
Author: User

Python Tkinter basic control usage, pythontkinter

This article demonstrates the usage of the basic Python Tkinter control and shares it with you for your reference. The specific method is as follows:

#-*-Coding: UTF-8-*-from Tkinter import * def btn_click (): b2 ['text'] = 'clicked' evalue = e. get () print 'btn Click and Entry value is % s' % evalue def btn_click_bind (event): print 'enter b2' def show_toplevel (): top = Toplevel () top. title ('window 2 ') Label (top, text =' This is window 2 '). pack () root = Tk () root. title ('window 1 ') # display built-in images # x = Label (root, bitmap = 'warning') l = Label (root, fg = 'red ', bg = 'blue', text = 'hangwei', width = 34, height = 10) l. pack () # command specifies the function B = Button (root, text = 'clickme ', command = btn_click) called by the Button) B ['width'] = 10b ['height'] = 2b. pack () # bind the join Button and function b2 = Button (root, text = 'clickme2') b2.configure (width = 10, height = 2, state = 'Disabled ') b2.bind ("<Enter>", btn_click_bind) b2.pack () # pop-up Toplevel window b3 = Button (root, text = 'showtoplevel', command = show_toplevel) b3.pack () # input box e = Entry (root, text = 'input your name') e. pack () # Password box epwd = Entry (root, text = 'input your pwd', show = '*') epwd. pack () # menu def menu_click (): print 'I am Menu' xmenu = Menu (root) submenu = menu (xmenu, tearoff = 0) for item in ['java ', 'cpp ', 'C', 'php']: xmenu. add_command (label = item, command = menu_click) for item in ['think in Java', 'java web', 'android']: submenu. add_command (label = item, command = menu_click) xmenu. add_cascade (label = 'progame', menu = submenu) # pop-up menu def pop (event): submenu. post (event. x_root, event. y_root) # obtain the coordinate def get_clickpoint (event): print event. x, event. y # framefor x in ['red', 'blue', 'yellow']: Frame (height = 20, width = 20, bg = x ). pack () root ['menu '] = xmenuroot. bind ('<Button-3>', pop) root. bind ('<Button-1>', get_clickpoint) root. mainloop ()

Shows the running effect:

I hope this article will help you with Python programming.


OptionMenu control of python Tkinter Library

'''Tkinter: OptionMenu '''
# OptionMenu is an optional menu, similar to the Combox function.
'''1. Create OptionMenu '''
#-*-Coding: cp936 -*-
From Tkinter import *
Root = Tk ()
V = StringVar (root)
V. set ('python ')
Om = OptionMenu (root, v, 'python', 'php', 'cpp ', 'C', 'java', 'javascript', 'vbscript ')
Om. pack ()

Root. mainloop ()
# The displayed text is automatically disconnected. The upper and lower lines are left-aligned, right-aligned, and center-aligned respectively.
#-*-Coding: cp936 -*-
From Tkinter import *
Root = Tk ()
V = StringVar (root)
# Create an OptionMenu Control
Om = OptionMenu (root,
V,
'Python ',
'Php ',
'Cpp ',
'C ',
'Java ',
'Javascript ',
'Vbscript'
)
Om. pack ()

Root. mainloop ()
# The creation of OptionMenu requires two necessary parameters. The variable bound to the current value is usually of the StringVar type; the other
# Provides an optional content list, which is specified by the OptionMenu variable parameter.

'''2. Set the display value of OptionMenu '''
# When OptionMenu is bound to a variable, the current value can be changed directly using the variable Assignment Method.
#-*-Coding: cp936 -*-
From Tkinter import *
Root = Tk ()
V = StringVar (root)
V. set ('vbscript ')
# Create an OptionMenu Control
Om = OptionMenu (root,
V,
'Python ',
'Php ',
'Cpp ',
'C ',
'Java ',
'Javascript ',
'Vbscript'
)
Om. pack ()
Print v. get ()

Root. mainloop ()
# Run the program. The default value of OptionMenu is "VBScript & quo ...... the remaining full text>

Python tkinter Text control, how to insert the Text and automatically move the cursor to the end of the line

I did not expect that tkinter will be used in China. It seems to be a technical party! Each control in tkinter has a variable. When this variable changes, the control content changes.

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.