Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science 111 Fundamentals of Programming I Text Areas

Similar presentations


Presentation on theme: "Computer Science 111 Fundamentals of Programming I Text Areas"— Presentation transcript:

1 Computer Science 111 Fundamentals of Programming I Text Areas
Radio Buttons and Check Buttons Prompter Boxes Working with Colors

2 class TaxCode(EasyFrame):
"""Application window for the tax calculator.""" RATE = .15 EXEMPTION_AMOUNT = def __init__(self): """Sets up the window and the widgets.""" EasyFrame.__init__(self, title = "Tax Calculator") . . . # The event handling method for the button def computeTax(self): """Obtains the data from the input fields and uses them to compute the tax, which is sent to the output field.""" income = self.incomeField.getNumber() exemptions = self.exField.getNumber() tax = max(0.0, (income - exemptions * TaxCode.EXEMPTION_AMOUNT) \ * TaxCode.RATE) self.taxField.setNumber(tax)

3 Widgets for Text I/O A text field (class TextField) is used for the I/O of one-line strings A text area (class TextArea) is used for the I/O of multiple lines of text See the breezypythongui site for details

4 Example: A Simple Text File Editor
Shows a text area to view and edit the contents of a text file Includes command buttons for opening and saving a file

5 App Window at Startup

6 Pop up an Open File Dialog

7 Load File Contents into Text Area

8 from breezypythongui import EasyFrame
import tkinter.filedialog class FileDialogDemo(EasyFrame): """Demonstrates file dialogs and a text area.""" def __init__(self): """Sets up the window and widgets.""" EasyFrame.__init__(self, "File Dialog Demo") self.outputArea = self.addTextArea("", row = 0, column = 0, columnspan = 2, width = 80, height = 15) self.addButton(text = "Open", row = 1, column = 0, command = self.openFile) self.addButton(text = "Save As...", row = 1, column = 1, command = self.saveFileAs)

9 from breezypythongui import EasyFrame
import tkinter.filedialog class FileDialogDemo(EasyFrame): """Demonstrates file dialogs and a text area.""" . . . # Event handling methods def openFile(self): """Pops up an open file dialog, and if a file is selected, displays its text in the text area.""" filetypes = [("Python files", "*.py"), ("Text files", "*.txt")] fileName = tkinter.filedialog.askopenfilename(parent = self, filetypes = filetypes) if fileName != "": file = open(fileName, "r") text = file.read() file.close() self.outputArea.setText(text) self.setTitle(fileName) def saveFileAs(self): """Pops up a save file dialog, and if a file is selected, saves the contents of the text area to the file.""" fileName = tkinter.filedialog.asksaveasfilename(parent = self) text = self.outputArea.getText() file = open(fileName, "w") file.write(text)

10 Radio Buttons Might have different tax rates depending on marital status: Single – 20% Married – 15% Divorced – 10%

11 Radio Buttons Might have different tax rates depending on marital status: Single – 20% Married – 15% Divorced – 10%

12 Radio Buttons Radio buttons belong to a group
Only one radio button in a group can be selected at any given time Add a radio button group to a window or panel first, and then add radio buttons to the group

13 Adding a Radio Button Group
The class EasyRadiobuttonGroup is a subclass of tkinter.Frame The method addRadiobuttonGroup allows spanning over rows and columns in the enclosing container’s grid, as well as an orientation (tkinter.VERTICAL or tkinter.HORIZONTAL) def addRadiobuttonGroup(self, row, column, rowspan = 1, columnspan = 1, orient = VERTICAL):

14 Adding a Radio Button The class EasyRadiobuttonGroup is a subclass of tkinter.Frame The method addRadiobutton requires a text argument and allows a command function whose default does nothing def addRadiobutton(self, text, command = lambda : 0):

15 Adding a Radio Button # Radio buttons for filing status
self.statusGroup = self.addRadiobuttonGroup(row = 0, column = 2, rowspan = 4) self.single = self.statusGroup.addRadiobutton(text = "Single") self.married = self.statusGroup.addRadiobutton(text = "Married") self.divorced = self.statusGroup.addRadiobutton(text = "Divorced") self.statusGroup["background"] = "pink" self.single["background"] = "pink" self.married["background"] = "pink" self.divorced["background"] = "pink" self.statusGroup.setSelectedButton(self.single)

16 Accessing a Radio Button
# The event handler method for the button def computeTax(self): """Obtains the data from the input field and uses them to compute the tax, which is sent to the output field.""" rate = 0 status = self.statusGroup.getSelectedButton() if status == self.single: rate = .20 elif status == self.married: rate = .15 elif status == self.divorced: rate = .10 income = self.incomeField.getNumber() numDependents = self.depField.getNumber() exemptionAmount = tax = (income - numDependents * exemptionAmount) * rate self.taxField.setNumber(max(tax, 0))

17 The value Attribute # The event handler method for the button
def computeTax(self): """Obtains the data from the input field and uses them to compute the tax, which is sent to the output field.""" rate = 0 status = self.statusGroup.getSelectedButton()["value"] if status == "Single": rate = .20 elif status == "Married": rate = .15 elif status == "Divorced": rate = .10 income = self.incomeField.getNumber() numDependents = self.depField.getNumber() exemptionAmount = tax = (income - numDependents * exemptionAmount) * rate self.taxField.setNumber(max(tax, 0))

18 Check Buttons Multiple check buttons may be selected at the same time

19 Check Buttons # The event handler method for the button
def __init__(self): """Sets up the window and widgets.""" EasyFrame.__init__(self, "Check Button Demo", background = "lightblue") self.chickCB = self.addCheckbutton(text = "Chicken", row = 0, column = 0) self.taterCB = self.addCheckbutton(text = "French fries", row = 0, column = 1) self.beanCB = self.addCheckbutton(text = "Green beans", row = 1, column = 0) self.sauceCB = self.addCheckbutton(text = "Applesauce", row = 1, column = 1)

20 Check Buttons def placeOrder(self):
"""Display a message box with the order information.""" message = "" if self.chickCB.isChecked(): message += "Chicken\n\n" if self.taterCB.isChecked(): message += "French fries\n\n" if self.beanCB.isChecked(): message += "Green beans\n\n" if self.sauceCB.isChecked(): message += "Applesauce\n" if message == "": message = "No food ordered!" self.messageBox(title = "Customer Order", message = message)

21 Prompter Boxes Pops up a dialog for text input; returns the text entered.

22 Prompter Boxes Pops up a dialog for text input; returns the text entered.

23 Prompter Boxes def getUserName(self):
"""Prompt user for name and display the input in the main window.""" text = self.prompterBox(title = "Input Dialog", promptString = "Your username:") self.label["text"] = "Hi, " + text + "!"

24 Working with Colors

25 Working with Colors

26 Use the colorchooser Module
import tkinter.colorchooser . # Event handling method def chooseColor(self): """Pops up a color chooser and outputs the results.""" colorTuple = tkinter.colorchooser.askcolor() if not colorTuple[0]: return ((r, g, b), hexString) = colorTuple self.r.setNumber(int(r)) self.g.setNumber(int(g)) self.b.setNumber(int(b)) self.hex.setText(hexString) self.canvas["background"] = hexString askcolor returns the value (None, None) if the user cancels the color chooser dialog

27 What Is a Hex String? A hex string represents a color as a six-digit hexadecimal number, with the form “#RRGGBB”

28 Programmer-Defined Classes Chapter 9
For Friday Programmer-Defined Classes Chapter 9


Download ppt "Computer Science 111 Fundamentals of Programming I Text Areas"

Similar presentations


Ads by Google