Guest User

Untitled

a guest
Apr 13th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.26 KB | None | 0 0
  1. Introducing the Longevity Program
  2. The Longevity program reveals the secret to living to the ripe old age of 100, if the user enters the secret password (the highly secure "secret"). The user enters the password in the text entry and then clicks the Submit button. If the password is correct, the program displays the key to longevity in the text box.
  3.  
  4. Setting Up the Program
  5. I set up the program just like the last few:
  6.  
  7. # Longevity
  8. # Demonstrates text and entry widgets, and the Grid layout manager
  9. # Michael Dawson - 6/7/03
  10.  
  11. from Tkinter import *
  12.  
  13. class Application(Frame):
  14. """ GUI application which can reveal the secret of longevity. """
  15. def __init__(self, master):
  16. """ Initialize the frame. """
  17. Frame.__init__(self, master)
  18. self.grid()
  19. self.create_widgets()
  20.  
  21. I import the Tkinter module and start to define the Application class. In the constructor method, I initialize the new Application object, make sure it will be visible, and invoke the object's create_widgets() method.
  22.  
  23. Placing a Widget with the Grid Layout Manager
  24. Next, I start the create_widgets() method and create a label that provides instructions to the user:
  25.  
  26. def create_widgets(self):
  27. """ Create button, text, and entry widgets. """
  28. # create instruction label
  29. self.inst_lbl = Label(self, text = "Enter password for the secret of longevity")
  30.  
  31. So far, nothing new. But in the next line, I use the Grid layout manager to be specific about the placement of this label:
  32.  
  33. self.inst_lbl.grid(row = 0, column = 0, columnspan = 2, sticky = W)
  34.  
  35. A widget object's grid() method can take values for many different parameters, but I only use four of them: row, column, columnspan, and sticky.
  36.  
  37. The row and column parameters take integers and define where an object is placed within its master widget. In this program, you can imagine the frame in the root window as a grid, divided into rows and columns. At each row and column inter-section is a cell, where you can place a widget. Figure 10.13 illustrates the placement of nine Button widgets, in nine different cells, using row and column numbers.
  38.  
  39.  
  40. Figure 10.13: Each button is located in a unique cell, based on a row and a column number.
  41. For my Label widget, I pass 0 to row and 0 to column, which puts the label in the upper-left corner of the frame.
  42.  
  43. If a widget is very wide (like the long instruction Label widget I have in this program), you may want to allow the widget to span more than one cell so that your other widgets are correctly spaced. The columnspan parameter lets you span a widget over more than one column. I pass 2 to this parameter to allow the long label to span two columns. This means that the label takes up two cells, the one at row 0, column 0, and the other at row 0, column 1. (You can also use the rowspan parameter to allow a widget to span more than one row.)
  44.  
  45. Even after you've established which cell (or cells) a widget occupies, you have the flexibility to justify the widget within the cell (or cells) by using the parameter sticky, which takes directions as values, including N, S, E, and W. A widget is moved to the quadrant of the cell (or cells) that corresponds to the direction. Since I pass W to sticky for the Label object, the label is forced to the west (left). Another way to say this is that the label is left-justified in its cells.
  46.  
  47. Next, I create a label that appears in the next row, left-justified:
  48.  
  49. # create label for password
  50. self.pw_lbl = Label(self, text = "Password: ")
  51. self.pw_lbl.grid(row = 1, column = 0, sticky = W)
  52.  
  53. Creating an Entry Widget
  54. Next, I create a new type of widget, an Entry widget:
  55.  
  56. # create entry widget to accept password
  57. self.pw_ent = Entry(self)
  58.  
  59. This code creates a text entry where the user can enter a password.
  60.  
  61. I position the Entry widget so that it's in the cell next to the password label:
  62.  
  63. self.pw_ent.grid(row = 1, column = 1, sticky = W)
  64.  
  65. Then, I create a button that lets the user submit his or her password:
  66.  
  67. # create submit button
  68. self.submit_bttn = Button(self, text = "Submit", command = self.reveal)
  69.  
  70. I bind the activation of the button with the reveal() method, which reveals the longevity secret, if the user has entered the correct password.
  71.  
  72. I place the button in the next row, all the way to the left:
  73.  
  74. self.submit_bttn.grid(row = 2, column = 0, sticky = W)
  75.  
  76.  
  77. Creating a Text Widget
  78. Next, I create a new type of widget, a Text widget:
  79.  
  80. # create text widget to display message
  81. self.secret_txt = Text(self, width = 35, height = 5, wrap = WORD)
  82.  
  83. I pass values to width and height to set the dimensions of the text box. Then I pass a value to the parameter wrap, which determines how text in the box is wrapped. Possible values for the parameter are WORD, CHAR, and NONE. WORD, the value I use for this Text widget, wraps entire words when you reach the right edge of the text box. CHAR wraps characters, meaning that when you get to the right edge of the text box, the next characters simply appears on the following line. NONE means no wrapping. As a result, you can only write text on the first line of the text box.
  84.  
  85. Next, I set the text box so that it's on the next row and spans two columns:
  86.  
  87. self.secret_txt.grid(row = 3, column = 0, columnspan = 2, sticky = W)
  88.  
  89. Getting and Inserting Text with Text-Based Widgets
  90. Next, I write the reveal() method, which tests to see if the user has entered the correct password. If so, the method displays the secret to a long life. Otherwise, the user is told that the password is incorrect.
  91.  
  92. The first thing I do is get the text in the Entry widget by invoking its get() method:
  93.  
  94. def reveal(self):
  95. """ Display message based on password. """
  96. contents = self.pw_ent.get()
  97.  
  98. The get() method returns the text in the widget. Both Entry and Text objects have a get() method.
  99.  
  100. I check to see if the text is equal to "secret". If so, I set message to the string describing the secret to living to 100. Otherwise, I set message to the string that tells the user that he or she entered the wrong password.
  101.  
  102. if contents == "secret":
  103. message = "Here's the secret to living to 100: live to 99 "\
  104. "and then be VERY careful."
  105. else:
  106. message = "That's not the correct password, so I can't share "\
  107. "the secret with you."
  108.  
  109.  
  110. Now that I've got the string that I want to show to the user, I need to insert it into the Text widget. First, I delete any text already in the Text widget by invoking its delete() method:
  111.  
  112. self.secret_txt.delete(0.0, END)
  113.  
  114. The delete() method can delete text from text-based widgets. The method can take a single index, or a beginning and an ending point. You pass floating-point numbers to represent a row and column number pair where the digits to the left of the decimal point is the row number and the digits to the right of the decimal point is the column number. For example, in the previous line of code, I pass 0.0 as the starting point, meaning that the method should delete text starting at row 0, column 0 (the absolute beginning) of the text box.
  115.  
  116. Tkinter provides several constants to help out with this type of method, such as END, which means the end of the text. So, this previous line of code deletes every-thing from the first position in the text box to the end. Both Text and Entry widgets have a delete() method.
  117.  
  118. Next, I insert the string I want to display into the Text widget:
  119.  
  120. self.secret_txt.insert(0.0, message)
  121.  
  122. The insert() method can insert a string into a text-based widget. The method takes an insertion position and a string. In the previous line of code, I pass 0.0 as the insertion position, meaning the method should start inserting at row 0, column 0. I pass message as the second value, so that the appropriate message shows up in the text box. Both Text and Entry widgets have an insert() method.
  123.  
  124. Wrapping Up the Program
  125. To wrap up the program, I create a root window and set its title and dimensions. Then I create a new Application object with the root window as its master. Finally, I begin the application by starting the window's event loop.
  126.  
  127. # main
  128. root = Tk()
  129. root.title("Longevity")
  130. root.geometry("250x150")
  131.  
  132. app = Application(root)
  133.  
  134. root.mainloop()
Add Comment
Please, Sign In to add comment