python entry get_来自另一个类的Python Tkinter Entry.get()

我最近遇到了一个问题,这个问题困扰着我的tkinter entry.get()函数,我把一个示例代码放在一起,这样你就可以看到我在做什么了,我有两个类,每个窗口一个类。在第一个窗口(主窗口)中有一个输入框,在第二个窗口中,我试图从第一个窗口获取输入框文本。在

代码如下:(试图从第二个类的第一个类获取输入框信息)from Tkinter import *

class window_1(object):

def __init__(self):

self.app = Tk()

self.app.title("Window One")

def entrybox(self):

self.ent = Entry(self.app) #This is the text i'm trying to get in 2nd class

def button(self):

def ODV(self):

class window_2(object):

def __init__(self):

self.app2 = Tk()

self.app2.title("Window Two")

def labels(self):

self.label_0 = Label(self.app2, text = "Name: ")

def info(self):

self.fetch_name = self.ent.get()#Here is my problem

def gridder(self):

self.label_0.grid(row = 0, column = 0)

self.fetch_name.grid(row = 0, column = 1)

rooter = window_2()

rooter.labels()

rooter.info()

rooter.gridder()

open_data_viewer = lambda: ODV(self)

self.but = Button(self.app, text = "Save", command = open_data_viewer)

def packer(self):

self.ent.pack(anchor = W)

self.but.pack(anchor = W)

def App_Runner(self):

self.app.mainloop()

root = window_1()

root.entrybox()

root.button()

root.packer()

root.App_Runner()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Preface 1 Chapter 1: Start your Engines 5 Introduction 5 Running a shortest Python program 6 Ensuring that the Python modules are present 7 A basic Tkinter program 9 Make a compiled executable under Windows and Linux 11 Chapter 2: Drawing Fundamental Shapes 15 Introduction 16 A straight line and the coordinate system 17 Draw a dashed line 18 Lines of varying styles with arrows and endcaps 20 A two segment line with a sharp bend 22 A line with a curved bend 23 Drawing intricate shapes – the curly vine 24 Draw a rectangle 27 Draw overlapping rectangles 28 Draw concentric squares 30 A circle from an oval 32 A circle from an arc 34 Three arc ellipses 35 Polygons 36 A star polygon 37 Cloning and resizing stars 39 Chapter 3: Handling Text 43 Introduction 43 Simple text 43 ii Table of Contents Text font type, size, and color 45 Alignment of text – left and right justify 49 All the fonts available on your computer 54 Chapter 4: Animation Principles 57 Introduction 57 Static shifting of a ball 58 Time-controlled shifting of a ball 59 Complete animation using draw-move-pause-erase cycles 62 More than one moving object 63 A ball that bounces 65 Bouncing in a gravity field 67 Precise collisions using floating point numbers 70 Trajectory tracing and ball-to-ball collisions 72 Rotating line 76 Trajectory tracing on multiple line rotations 78 A rose for you 82 Chapter 5: The Magic of Color 85 Introduction 85 A limited palette of named colors 86 Nine ways of specifying color 90 A red beachball of varying hue 91 A red color wedge of graded hue 94 Newton's grand wheel of color mixing 96 The numerical color mixing matching palette 101 The animated graded color wheel 106 Tkinter's own color picker-mixer 110 Chapter 6: Working with Pictures 113 Opening an image file and discovering its attributes 114 Open, view, and save an image in a different file format 117 Image format conversion for JPEG, PNG, TIFF, GIF, BMP 118 Image rotation in the plane of the image 120 Image size alteration 121 Correct proportion image resizing 123 Separating one color band in an image 124 Red, green, and blue color alteration in images 125 Slider controlled color manipulation 127 Combining images by blending 130 Blending images by varying percentages 131 Make a composite image using a mask image 132 iii Table of Contents Offset (roll) image horizontally and vertically 134 Flip horizontally, vertically, and rotate 134 Filter effects: blur, sharpen, contrast, and so on 135 Chapter 7: Combining Raster and Vector Pictures 139 Simple animation of a GIF beach ball 140 The vector walking creature 141 Bird with shoes walking in the Karroo 145 Making GIF images with transparent backgrounds using GIMP 149 Diplomat walking at the palace 152 Spider in the forest 156 Moving band of images 160 Continuous band of images 162 Endless background 164 Chapter 8: Data In and Data Out 167 Introduction 167 Creation of a new file on a hard drive 168 Writing data to a newly-created file 169 Writing data to multiple files 169 Adding data to existing files 170 Saving a Tkinter-drawing shape to disk 171 Retrieving Python data from disk storage 172 Simple mouse input 173 Storing and retrieving a mouse-drawn shape 174 A mouse-line editor 177 All possible mouse actions 181 Chapter 9: Exchanging Inkscape SVG Drawings with Tkinter Shapes 185 Introduction 185 The structure of an SVG drawing 186 Tracing the shape of an image in Inkscape 189 Converting an SVG path into a Tkinter Line 194 Chapter 10: GUI Construction: Part 1 199 Introduction 199 Widget configuration – a label 200 Button focus 201 The simplest push button with validation 203 A data entry box 204 Colored button causing a message pop-up 207 Complex interaction between buttons 208 Images on buttons and button packing 211 iv Table of Contents Grid Geometry Manager and button arrays 213 Drop-down menus to select from a list 215 Listbox variable selection 216 Text in a window 218 Chapter 11: GUI Construction: Part 2 219 Introduction 219 The Grid Layout Geometry Manager 220 The Pack Geometry Manager 222 Radiobuttons to select one from many 223 Checkbuttons (Tickboxes) to select some of many 224 Key-stroke event handling 226 Scrollbar 227 Custom DIY controller widgets 228 Organizing widgets inside frames 232 Appendix: Quick tips for running Python programs in Microsoft Windows 235 Running Python programs in Microsoft Windows 235 Where will we find the windows installer? 235 Do we have to use Python version 2.7? 236 Why do we get "python is not recognized…"? 236 Index 239 Preface Python 2.6 Graphics Cookbook is a collection of straightforward recipes and illustrative screenshots for creating and animating graphic objects using the Python language. This book makes the process of developing graphics interesting and entertaining by working in a graphic workspace, without the burden of mastering complicated language definitions and opaque examples. What this book covers Chapter 1, Start your Engines: This chapter explains how to acquire and install the Python interpreter, for MS Windows or Linux as well as how to verify that Python is correctly installed. This chapter explains how to create complete working programs that can be run on client computers that do not have Python installed. Chapter 2, Drawing Fundamental Shapes: This shows how to create all the fundamental graphic elements including lines, circles, ovals, rectangles, polygons, and complex curves. Simple examples are provided to demonstrate how to draw the elementary shapes. The examples also provide a ready for reference for later use. Chapter 3, Handling Text: This chapter demonstrates how to control font size, color, and position using any of the font typefaces installed on the specific operating system being used. A simple means of discovering and demonstrating all available fonts on the operating system is shown. Chapter 4, Animation Principles: This chapter starts with examples of simple sequences of a circle in different positions and systematically progresses to smoothly-moving animations of elastic balls bouncing inside a gravity field. Chapter 5, The Magic of Color: This chapter begins with the assembling of color palettes using color names recognizable to Python. The way colors are constructed using numbers to mix controlled amounts of red, green, and blue is explained. Tools for matching colors to any sample are constructed. This chapter demonstrates how to vary shadings of one color into another. Download from Wow! eBook <www.wowebook.com> Preface Chapter 6, Working with Pictures: This chapter reveals how to acquire and use the Python Imaging Library to manipulate photo images. It also shows methods of image format conversion, re-sizing, rotating, color transforming, and complex filtering. Chapter 7, Combining Vector and Raster Images: This chapter demonstrates the ways of combining animated vector graphics with photographic images to produce complex animations. Chapter 8, Data in and Data Out: This chapter starts with basic storing and retrieving of files to a hard drive and progresses to the construction of programs that are tools for creating, storing, and retrieving free-form shapes drawn using a mouse. Chapter 9, Exchanging Inkscape SVG Drawings with Tkinter Shapes: This chapter shows in detail how to use the Inkscape drawing tool to convert shapes traced from a photographic image into a sequence of points which reproduce the shape in Python. Once a line is expressed as a Python sequence, it can be transformed numerically in many ways. Chapter 10, GUI Construction: Part 1: This chapter provides basic examples of how to create buttons, data entry boxes, drop-down menus, list-boxes, and text labels. It also covers how to customize button appearance. Chapter 11, GUI Construction: Part 2: Here the Grid Layout Manager and the Pack Layout Manager are explained and demonstrated. Examples of radio buttons, check buttons, scrollbars, frames, and keystroke event coding are given. It also shows how to construct widgets using graphic elements on a canvas. Appendix, Quick tips for running Python programs in Microsoft Windows: This gives explanations of how to overcome some of the difficulties a new python programmer might encounter when trying to use Python in Windows. What you need for this book To run the code in this book, the reader will need a Linux operating system or Microsoft Windows, and some way of downloading Python, the Python Imaging Library, and Inkscape from the internet. All these applications are free and open source. The code has been developed on Linux Ubuntu version 9.04, Microsoft Windows XP, and Windows 7. Who this book is for This book is for Python programmers wanting simple, clear examples of graphic programming using Python. The examples are aimed at anyone wanting to use graphic elements and images inside Python programs with the minimum of complexity. The intended reader ranges from scholars and teachers to engineers and technicians
CHAPTER 1 About Python ............................................................................................1 What Is Python? ................................................................................................................1 A Brief History of Python ................................................................................................2 Interpreters Versus Compilers .......................................................................................5 When to Use (or Not Use) an Interpreted Language .........................................8 Understanding Bytecodes ......................................................................................10 Why Use Python? ...........................................................................................................11 Object-Oriented ........................................................................................................11 Cross Platform ..........................................................................................................11 Broad User Base .......................................................................................................11 Well Supported in Third-Party Tools ...................................................................12 Good Selection of Tools Available ........................................................................12 Good Selection of Pre-built Libraries ..................................................................12 Where Is Python Used? .................................................................................................13 How Is Python Licensed? ..............................................................................................13 Where Do I Get Python? ...............................................................................................14 Installing Python ............................................................................................................14 Getting Information on Python ..................................................................................16 Python Communities .....................................................................................................17 Other Software ................................................................................................................18 And Now for Something Completely Different… ....................................................18 CHAPTER 2 Python Language Overview .................................................................19 Python Syntax .................................................................................................................20 Comments .................................................................................................................20 Indentation ...............................................................................................................20 Contents TABLE OF} vii Q Q Q Python Reserved Words ................................................................................................24 Decision Making and Iteration Keywords ..........................................................25 Debugging Keywords ..............................................................................................27 Package and Module Handling Keywords .........................................................27 Exception Handling Keywords ..............................................................................29 General Language Keywords .................................................................................31 Other Keywords ........................................................................................................32 Variable Usage ................................................................................................................34 The Continuation Variable ....................................................................................36 Watching Out for Spelling Mistakes! ..................................................................37 Predicates .........................................................................................................................38 Identifier Scope ...............................................................................................................39 Operators .........................................................................................................................42 Modulo Operator .....................................................................................................44 Exponential Operator .............................................................................................46 Logical Operators ....................................................................................................46 Comparative Operators ..........................................................................................49 Bitwise Operators ....................................................................................................51 Membership Operators and String Operators ..................................................53 Identity Operators ...................................................................................................53 In Conclusion ..................................................................................................................53 CHAPTER 3 Tools ..........................................................................................................55 IDLE ...................................................................................................................................55 File Menu ...................................................................................................................57 The Path Browser Dialog ........................................................................................62 Edit Menu ..................................................................................................................64 Shell Menu .................................................................................................................70 Debug Menu ..............................................................................................................71 The Edit Window ......................................................................................................79 Format Menu ............................................................................................................80 CONTENTS viii Q Q Q Command Line Compiler ..............................................................................................90 Creating Python Files ....................................................................................................93 Documentation ...............................................................................................................95 In Conclusion ..................................................................................................................96 CHAPTER 4 Data Types ...............................................................................................97 Numeric Types ................................................................................................................98 Integers ......................................................................................................................98 Demonstrating Long Integers ...............................................................................99 Octal and Hexadecimal ........................................................................................100 Floating Point Numbers .............................................................................................101 Strings ............................................................................................................................103 String Variables .....................................................................................................103 Concatenating Strings .........................................................................................106 Repeating Strings ..................................................................................................107 Substrings ...............................................................................................................108 Slicing ......................................................................................................................110 String Functions ....................................................................................................111 String Constants ....................................................................................................112 Conversion Functions ...........................................................................................114 Search Functions ...................................................................................................118 Formatting Functions ..........................................................................................120 Escape Sequences ..................................................................................................121 Sequences ......................................................................................................................122 Lists ..........................................................................................................................123 Shared References .................................................................................................128 Tuples .......................................................................................................................128 Dictionaries ............................................................................................................132 Advanced Type .............................................................................................................136 Classes and Objects ..............................................................................................136 Complex Type .........................................................................................................137 Generator Type ......................................................................................................138 CONTENTS ix Q Q Q None Type ...............................................................................................................139 Unicode Type ..........................................................................................................140 In Conclusion ................................................................................................................141 CHAPTER 5 Control Flow .........................................................................................143 Conditionals ..................................................................................................................144 The if Statement ..................................................................................................144 The elif Statement .............................................................................................147 The else Statement .............................................................................................149 Wrapping Up the Conditionals: A Cool Example ...........................................150 Loops ..............................................................................................................................153 The for Loop ..........................................................................................................153 The while Loop .....................................................................................................161 In Conclusion ................................................................................................................164 CHAPTER 6 Input and Output ................................................................................165 User Input ......................................................................................................................165 The input Function ..............................................................................................166 The raw_input Function ....................................................................................168 User Output ...................................................................................................................170 Formatting ..............................................................................................................172 File Input .......................................................................................................................175 File Output ....................................................................................................................177 Closing Files ..................................................................................................................179 Positioning in Files ......................................................................................................180 Directories and Files ...................................................................................................183 The stat Module: File Statistics ..............................................................................186 Command Line Arguments ........................................................................................190 Pickle ..............................................................................................................................192 In Conclusion ................................................................................................................195 CONTENTS x Q Q Q CHAPTER 7 Functions and Modules .....................................................................197 What Is a Function? ....................................................................................................197 Defining Functions in Python ...................................................................................197 What Are Arguments? ................................................................................................200 How Do You Pass an Argument to a Function? ....................................................201 Default Arguments ......................................................................................................203 Variable Default Arguments .....................................................................................205 Keyword Arguments ....................................................................................................206 Returning Values from Functions ............................................................................207 Returning Multiple Values from Functions ...........................................................209 Recursive Functions ....................................................................................................210 Passing Functions as Arguments .............................................................................212 Lambda Functions .......................................................................................................213 Variable Numbers of Arguments to a Function ....................................................215 Variable Scope in Functions ......................................................................................216 Using Modules ..............................................................................................................218 In Conclusion ................................................................................................................219 CHAPTER 8 Exception Handling ............................................................................221 Looking at Exceptions in Python ..............................................................................222 Traceback Example ......................................................................................................223 Understanding Tracebacks ........................................................................................224 Exceptions .....................................................................................................................225 Catching Exceptions with try..except .........................................................226 Multiple except Clauses .....................................................................................229 Blank except Clauses ..........................................................................................231 The else Clauses .........................................................................................................232 The finally Clause ....................................................................................................234 Raising Your Own Exceptions ...................................................................................235 Exception Arguments .................................................................................................237 User-Defined Exceptions ............................................................................................238 Working with the Exception Information ..............................................................239 CONTENTS xi Q Q Q exc_type ................................................................................................................239 exc_value ..............................................................................................................240 Using the with Clause for Files ................................................................................243 Re-throwing Exceptions .............................................................................................244 In Conclusion ................................................................................................................246 CHAPTER 9 Object-Oriented Programming .........................................................247 A Brief History of OOP ................................................................................................247 What Is an Object? ......................................................................................................248 Why Do We Use Objects? ...........................................................................................249 Reuse ........................................................................................................................249 Ease in Debugging .................................................................................................250 Maintainability ......................................................................................................250 The Attributes of Object-Oriented Development .................................................251 Abstraction .............................................................................................................251 Data Hiding ............................................................................................................252 Inheritance .............................................................................................................253 Polymorphism ........................................................................................................255 Terminology ..................................................................................................................256 Class .........................................................................................................................256 Object .......................................................................................................................256 Attribute ..................................................................................................................257 Method ....................................................................................................................258 Message Passing ....................................................................................................259 Event Handling ......................................................................................................260 Derivation ...............................................................................................................260 Coupling ..................................................................................................................261 Cohesion ..................................................................................................................261 Constants ................................................................................................................261 Other Concepts .............................................................................................................262 In Conclusion ................................................................................................................262 CONTENTS xii Q Q Q CHAPTER 10 Classes and Objects in Python ..........................................................265 Python Classes ..............................................................................................................265 Properties ......................................................................................................................267 Attribute Modifying Functions .................................................................................272 Private Attributes ........................................................................................................274 Doc Strings ....................................................................................................................275 Properties ......................................................................................................................277 The self Object ...........................................................................................................279 Methods .........................................................................................................................281 Special Methods ...........................................................................................................283 Initialization ...........................................................................................................283 Termination ............................................................................................................284 String Conversion ..................................................................................................285 Inheritance ....................................................................................................................287 Multiple Inheritance ...................................................................................................291 Using super ..................................................................................................................293 Polymorphism ..............................................................................................................295 Exception Classes .........................................................................................................297 Iterators .........................................................................................................................299 Operator Overloading .................................................................................................301 In Conclusion ................................................................................................................304 CHAPTER 11 The Python Library ..............................................................................305 Containers .....................................................................................................................305 Working with the deque Class ...........................................................................306 Math ................................................................................................................................312 Complex Math ..............................................................................................................313 Types ...............................................................................................................................315 Strings ............................................................................................................................318 Regular Expressions ....................................................................................................319 Patterns ...................................................................................................................320 Special Sequence Characters ..............................................................................323 CONTENTS xiii Q Q Q Compiling Regular Expressions .........................................................................323 Matching Strings ...................................................................................................324 Meta Characters ....................................................................................................326 Grouping .................................................................................................................327 System ............................................................................................................................328 Random Number Generation ....................................................................................330 Dates and Times ...........................................................................................................331 Creating a New Time ............................................................................................332 Time Operations ....................................................................................................332 Creating a New Date .............................................................................................333 Date Operations ....................................................................................................333 Time Zone Information ........................................................................................335 Operating System Interface .......................................................................................336 System Information ..............................................................................................336 Process Management ...........................................................................................337 In Conclusion ................................................................................................................341 CHAPTER 12 The GUI — TkInter ...............................................................................343 What Is TkInter? ...........................................................................................................343 Terms and Conditions .................................................................................................343 Event Handling ......................................................................................................344 Callbacks .................................................................................................................344 Widgets ....................................................................................................................345 Layout Managers ...................................................................................................345 Working with TkInter .................................................................................................346 Creating a Label ...........................................................................................................347 Frame Widgets and Centering ..................................................................................349 An Application with a Button ...................................................................................351 Working with Entry Fields and Grid Layouts ........................................................353 Creating a Class to Handle User Interfaces ...........................................................356 Working with List Boxes ............................................................................................358 Scrolling a List Box ................................................................................................361 CONTENTS xiv Q Q Q Menus .............................................................................................................................363 Context Menus .............................................................................................................366 Scale Widgets ................................................................................................................367 RadioButtons and CheckButton ...............................................................................370 Text Widgets .................................................................................................................373 In Conclusion ................................................................................................................375 CHAPTER 13 The Web Server—Apache ...................................................................377 Setting Up Apache .......................................................................................................377 Testing Apache .............................................................................................................378 Your First Python CGI Script: Hello Apache ...........................................................379 Examining the Hello Python Script ...................................................................380 The cgi-bin Directory ............................................................................................381 A Script for Displaying the Environment ...............................................................382 Receiving Data from an HTML File ..........................................................................384 Sending Data to an HTML File ..................................................................................387 How It All Works ...................................................................................................390 Dynamic HTML Displays Based on User Input ......................................................391 HTML Elements ............................................................................................................396 Cookies ...........................................................................................................................399 Uploading Files ............................................................................................................402 Redirection ....................................................................................................................403 Error Handling ..............................................................................................................405 In Conclusion ................................................................................................................406 CHAPTER 14 Working with Databases ...................................................................407 What Is a Database? ...................................................................................................407 Simple Database Terminology ...........................................................................408 What Is MySQL? ...........................................................................................................409 Downloading and Installing ......................................................................................409 Creating a New Database ..........................................................................................410 Creating a New User ....................................................................................................414 CONTENTS xv Q Q Q Opening an Existing Database .................................................................................415 Writing to a Database ................................................................................................417 Reading from a Database ..........................................................................................421 Updating a Database ..................................................................................................424 Deleting from a Database ..........................................................................................427 Searching a Database .................................................................................................430 In Conclusion ................................................................................................................436 CHAPTER 15 Putting It All Together .......................................................................437 Designing the Application .........................................................................................437 Program Flow .........................................................................................................437 User Interface Design ...........................................................................................438 Database Design ...................................................................................................439 Implementing the Database Tables ........................................................................440 Implementing the Forms ...........................................................................................442 Adding Reviews ............................................................................................................449 Adding the Review to the Database ........................................................................452 Listing the Reviews ......................................................................................................456 Deleting Books .............................................................................................................459 In Conclusion ................................................................................................................462 CHAPTER 16 Python and Graphics ..........................................................................463 The PIL Library ..............................................................................................................463 Downloading ..........................................................................................................464 Installing .................................................................................................................464 Verifying Your Installation ..................................................................................465 Creating a New Image ................................................................................................465 Function Parameters ............................................................................................466 Drawing on the Image ................................................................................................467 Drawing the Image ...............................................................................................468 Displaying the Image ...........................................................................................470 CONTENTS xvi Q Q Q

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值