본문 바로가기

카테고리 없음

Tkinter Tutorial Python Pdf Generation

Tkinter is a Python wrapper for Tcl/Tk providing a cross-platform GUI toolkit. On Windows, it comes bundled with Python; on other operating systems, it can be installed. The set of available widgets is smaller than in some other toolkits, but since Tkinter widgets are extensible, many of the missing compound widgets can be created using the extensibility, such as combo box and scrolling pane.IDLE, Python's Integrated Development and Learning Environment, is written using Tkinter and is often distributed with Python. You can learn about features of Tkinter by playing around with menus and dialogs of IDLE. For instance, Options Configure IDLE.

Dialog shows a broad variety of GUI elements including tabbed interface. You can learn about programming using Tkinter by studying IDLE source code, which, on Windows, is available e.g.

In C:Program FilesPython27Libidlelib.Python 3: The examples on this page are for Python 2. In Python 3, what was previously module Tkinter is tkinter, what was tkMessageBox is messagebox, etc. Import Tkinter, tkMessageBox Tkinter. Withdraw # Workaround: Hide the window answer = tkMessageBox. Askokcancel ( 'Confirmation', 'File not saved. ) answer = tkMessageBox. Askyesno ( 'Confirmation', 'Do you really want to delete the file?'

Tkinter Tutorial Python Pdf Generation

) # Above, 'OK' and 'Yes' yield True, and 'Cancel' and 'No' yield False tkMessageBox. Showwarning ( 'Warning', 'Timeout has elapsed.' ) tkMessageBox. Showwarning ( 'Warning', 'Timeout has elapsed.'

, icon = tkMessageBox. ERROR ) tkMessageBox. Showerror ( 'Warning', 'Timeout has elapsed.' )Links:., infohost.nmt.edu., effbot.orgFile dialog File dialogs can be created as follows. Import Tkinter, tkFileDialog Tkinter. Withdraw # Workaround: Hide the window filename1 = tkFileDialog.

Askopenfilename filename2 = tkFileDialog. Askopenfilename ( initialdir = r 'C:Users' ) filename3 = tkFileDialog. Asksaveasfilename filename4 = tkFileDialog. Asksaveasfilename ( initialdir = r 'C:Users' ) if filename1 ': for line in open ( filename1 ): # Dummy reading of the file dummy = line. Rstrip Links:., infohost.nmt.edu., effbot.org., tkinter.unpythonic.netRadio button A radio button can be used to create a simple choice dialog with multiple options.

From Tkinter import. master = Tk choices = ( 'Apple', 'a' ), ( 'Orange', 'o' ), ( 'Pear', 'p' ) defaultChoice = 'a' userchoice = StringVar userchoice. Set ( defaultChoice ) def cancelAction : userchoice. Set ( ' ); master. Quit Label ( master, text = 'Choose a fruit:' ). Pack for text, key in choices: Radiobutton ( master, text = text, variable = userchoice, value = key ). Pack ( anchor = W ) Button ( master, text = 'OK', command = master.

Pack ( side = LEFT, ipadx = 10 ) Button ( master, text = 'Cancel', command = cancelAction ). Pack ( side = RIGHT, ipadx = 10 ) mainloop if userchoice. Get ': print userchoice. Get # 'a', or 'o', or 'p' else: print 'Choice canceled.'

An alternative to radio button that immediately reacts to button press. From Tkinter import. import os buttons = ( 'Users', r 'C:Users' ), ( 'Windows', r 'C:Windows' ), ( 'Program Files', r 'C:Program Files' ) master = Tk def open ( filePath ): def openInner : os. Chdir ( filePath ) # Cross platform #os.system('start ' '+filePath+') # Windows master. Quit return openInner Label ( master, text = 'Choose a fruit:' ). Pack for buttonLabel, filePath in buttons: Button ( master, text = buttonLabel, command = open ( filePath )). Pack ( anchor = W ) mainloop Links:., infohost.nmt.edu., effbot.orgList box A list box can be used to create a simple multiple-choice dialog.

From Tkinter import. master = Tk choices = 'Apple', 'Orange', 'Pear' canceled = BooleanVar def cancelAction : canceled. Set ( True ); master. Quit Label ( master, text = 'Choose a fruit:' ). Pack listbox = Listbox ( master, selectmode = EXTENDED ) # Multiple options can be chosen for text in choices: listbox. Insert ( END, text ) listbox. Pack Button ( master, text = 'OK', command = master.

Pack ( side = LEFT, ipadx = 10 ) Button ( master, text = 'Cancel', command = cancelAction ). Pack ( side = RIGHT, ipadx = 10 ) mainloop if not canceled. Get : print listbox. Curselection # A tuple of choice indices starting with 0 # The above is a tuple even if selectmode=SINGLE if '0' in listbox.

Curselection : print 'Apple chosen.' If '1' in listbox. Curselection : print 'Orange chosen.'

If '2' in listbox. Curselection : print 'Pear chosen.'

Else: print 'Choice canceled.' Links:., infohost.nmt.edu., effbot.org., code.activestate.comCheckbox Checkbox or check button can be created as follows. From Tkinter import. root = Tk def mycommand : print 'Chosen.' Menubar = Menu ( root ) menu1 = Menu ( menubar, tearoff = 0 ) menu1. Addcommand ( label = 'New', command = mycommand ) menu1.

Addcommand ( label = 'Clone', command = mycommand ) menu1. Addseparator menu1. Addcommand ( label = 'Exit', command = root. Quit ) menubar.

Addcascade ( label = 'Project', menu = menu1 ) menu2 = Menu ( menubar, tearoff = 0 ) menu2. Addcommand ( label = 'Oval', command = mycommand ) menu2. Addcommand ( label = 'Rectangle', command = mycommand ) menubar. Addcascade ( label = 'Shapes', menu = menu2 ) root. Config ( menu = menubar ) mainloop Links:., infohost.nmt.edu., effbot.orgLabelFrame A frame around other elements can be created using LabelFrame widget as follows. From Tkinter import. root = Tk options = 'Apple', 'Orange', 'Pear' selectedOption = StringVar selectedOption.

Python Tkinter Examples Pdf

Set ( 'Apple' ) # Default OptionMenu ( root, selectedOption,. options ).

Pack mainloop print selectedOption. Get # The text in the options listLinks:., infohost.nmt.edu., effbot.orgText Text widget is a more complex one, allowing editing of both plain and formatted text, including multiple fonts.Example to be added.Links:., infohost.nmt.edu., effbot.orgTcl/Tk version The Windows installer for Python 2.3 ships with Tcl/Tk 8.4.3. You can find out about the version.

Options For Python GUI Programming a. TkinterPython ships with the Tk GUI toolkit. Tkinter is an interface to this. PyQtA Python binding of the cross-platform GUI toolkit Qt, PyQt is implemented as a plug-in for Python. WxPythonwxWidgets is a cross-platform GUI API for Python; wxPython is a wrapper for this. What is the Python Tkinter?Tkinter in Python GUI Programming is standard Python GUI library. It gives us an object-oriented interface to the Tk GUI toolkit.So, let’s start Python graphics, let’s create a simple GUI application:.

Import the module Tkinter. Create the main window for the application.

Add a widget or more. Enter the main event loop to deal with an event when it is triggered. import tkinter top=tkinter.Tk top.mainloopNow, this creates the following window. An output of Adding a Menu Button i. MenuThis lets us put all kinds of menus in our application. Python GUI Programming with TkinterSo, this was all about Python GUI Programming. Hope you like our explanation of Tkinter Tutorial in Python 3.5.

ConclusionHence, we completed the Python Tkinter tutorial. Now, it is time to get started with Python GUI programming.

Python Tkinter Tutorial Pdf

In addition, we saw Python GUI Programming and alternative for Python GUI Programming. Moreover, we discussed Tkinter and widgets. At last, we learned different kind of widgets that Tinker provides. Furthermore, for any query regarding Python GUI Programming with Tkinter, feel free to ask in the comment section.