Python Tkinter入门教程:打造简单应用

需积分: 50 2 下载量 40 浏览量 更新于2024-07-22 收藏 1.07MB PDF 举报
"python tkinter 教程 辛星 Tkinter应用开发" 这篇教程是关于Python的图形用户界面库Tkinter的入门指南,由作者辛星在2014年编写。虽然较早,但它为初学者提供了对Tkinter基础知识的简单介绍。Tkinter是Python的标准库之一,用于创建跨平台的GUI应用程序。它源于Tcl/Tk库,但具有Python的接口,使得Python程序员可以直接使用。 在教程的第一章"走进Tkinter的大门"中,作者指出Tkinter不是通过拖拽方式构建界面,而是需要程序员手动编写代码来构建图形界面,这可能对于一些习惯于可视化界面设计的开发者来说增加了难度。然而,Tkinter作为Python的标准库,其跨平台性以及与Python的紧密集成使得它仍然是一个有价值的学习对象。 教程后续章节逐步介绍了Tkinter的核心组件和功能: 1. **标签(Labels)与按钮(Buttons)**:这两者是GUI中最基础的元素。标签通常用于显示静态信息,而按钮则用于触发用户的交互行为。 2. **输入框(Entry)**:输入框允许用户输入文本数据,是收集用户信息的重要控件。 3. **菜单(Menus)**:菜单栏提供了一种组织功能并使用户能够访问应用程序不同部分的方式。 4. **消息(Messages)**:用于显示较长的文本或警告信息,不同于标签的单行显示,它可以容纳更多内容。 5. **完整的窗口应用程序**:这一部分将展示如何组合上述元素创建一个完整的、功能性的GUI应用。 6. **布局管理(Layout Management)**:Tkinter提供了多种布局管理器,如网格(Grid)、 pack 和 place,用于控制组件在窗口中的排列和大小调整。 7. **总结**:在教程的最后,作者可能回顾了所学内容,并鼓励读者进一步探索和实践以深化理解。 需要注意的是,由于教程编写于2014年,那时使用的是Python3.2,可能与当前的Python版本存在差异,但基本概念和使用方法应该大同小异。若要获取最新和更详尽的信息,建议读者通过搜索引擎查找更新的Tkinter教程和资源。 这个教程为Python初学者提供了一个很好的起点,让他们了解如何使用Tkinter构建简单的图形用户界面。随着对Tkinter的深入理解和实践,开发者可以创造出更加复杂和专业的GUI应用。
2361 浏览量
Abstract Describes the Tkinter widget set for constructing graphical user interfaces (GUIs) in the Python programming language. This publication is available in Web form1 and also as a PDF document2. Please forward any comments to tcc-doc@nmt.edu. Table of Contents 1. What is Tkinter?.......................................................................................................................3 2. A minimal application..............................................................................................................3 3. Definitions..............................................................................................................................4 4. Layout management.................................................................................................................5 4.1. The .grid() method....................................................................................................5 4.2. Other grid management methods...................................................................................6 4.3. Configuring column and row sizes.................................................................................7 4.4. Making the root window resizeable................................................................................8 5. Standard attributes...................................................................................................................8 5.1. Dimensions...................................................................................................................9 5.2. The coordinate system...................................................................................................9 5.3. Colors...........................................................................................................................9 5.4. Type fonts...................................................................................................................10 5.5. Anchors......................................................................................................................11 5.6. Relief styles.................................................................................................................12 5.7. Bitmaps.......................................................................................................................12 5.8. Cursors.......................................................................................................................12 5.9. Images........................................................................................................................14 5.10. Geometry strings........................................................................................................14 5.11. Window names...........................................................................................................15 5.12. Cap and join styles.....................................................................................................15 5.13. Dash patterns.............................................................................................................16 5.14. Matching stipple patterns............................................................................................16 6. The Button widget................................................................................................................17 7. The Canvas widget................................................................................................................19 7.1. Canvas coordinates......................................................................................................20 7.2. The Canvas display list................................................................................................20 7.3. Canvas object IDs........................................................................................................21 7.4. Canvas tags................................................................................................................21 1http://www.nmt.edu/tcc/help/pubs/tkinter/ 2http://www.nmt.edu/tcc/help/pubs/tkinter/tkinter.pdf 1 Tkinter reference New Mexico Tech Computer Center 7.5. CanvastagOrId arguments......................................................................................21 7.6. Methods on Canvas widgets........................................................................................21 7.7. Canvas arc objects.......................................................................................................26 7.8. Canvas bitmap objects.................................................................................................28 7.9. Canvas image objects..................................................................................................29 7.10. Canvas line objects.....................................................................................................29 7.11. Canvas oval objects....................................................................................................31 7.12. Canvas polygon objects..............................................................................................32 7.13. Canvas rectangle objects.............................................................................................34 7.14. Canvas text objects.....................................................................................................35 7.15. Canvas window objects..............................................................................................36 8. The Checkbutton widget......................................................................................................37 9. The Entry widget..................................................................................................................40 9.1. Scrolling an Entry widget............................................................................................43 10. The Frame widget................................................................................................................43 11. The Label widget................................................................................................................44 12. The LabelFrame widget......................................................................................................46 13. The Listbox widget............................................................................................................48 13.1. Scrolling a Listbox widget........................................................................................52 14. The Menu widget..................................................................................................................52 14.1. Menu item creation (coption) options.........................................................................55 14.2. Top-level menus.........................................................................................................56 15. The Menubutton widget......................................................................................................57 16. The Message widget............................................................................................................59 17. The OptionMenu widget.......................................................................................................60 18. The PanedWindow widget....................................................................................................61 18.1. PanedWindow child configuration options...................................................................63 19. The Radiobutton widget....................................................................................................64 20. The Scale widget................................................................................................................67 21. The Scrollbar widget........................................................................................................70 21.1. The Scrollbarcommand callback............................................................................72 21.2. Connecting a Scrollbar to another widget................................................................73 22. The Spinbox widget............................................................................................................73 23. The Text widget..................................................................................................................78 23.1. Text widget indices...................................................................................................80 23.2. Text widget marks....................................................................................................81 23.3. Text widget images...................................................................................................82 23.4. Text widget windows...............................................................................................82 23.5. Text widget tags.......................................................................................................82 23.6. Setting tabs in a Text widget......................................................................................83 23.7. The Text widget undo/redo stack..............................................................................83 23.8. Methods on Text widgets..........................................................................................84 24. Toplevel: Top-level window methods..................................................................................91 25. Universal widget methods.....................................................................................................93 26. Standardizing appearance...................................................................................................101 26.1. How to name a widget class......................................................................................102 26.2. How to name a widget instance.................................................................................102 26.3. Resource specification lines.......................................................................................102 26.4. Rules for resource matching......................................................................................103 27. Connecting your application logic to the widgets...................................................................104 28. Control variables: the values behind the widgets...................................................................104 29. Focus: routing keyboard input.............................................................................................106 New Mexico Tech Computer Center Tkinter reference 2 30. Events................................................................................................................................107 30.1. Levels of binding......................................................................................................108 30.2. Event sequences.......................................................................................................109 30.3. Event types..............................................................................................................109 30.4. Event modifiers........................................................................................................110 30.5. Key names...............................................................................................................111 30.6. Writing your handler: The Event class......................................................................113 30.7. The extra arguments trick..........................................................................................115 30.8. Virtual events...........................................................................................................116 31. Pop-up dialogs....................................................................................................................116 31.1. The tkMessageBox dialogs module..........................................................................116 31.2. The tkFileDialog module.....................................................................................118 31.3. The tkColorChooser module.................................................................................119