Question

Exercise 3) Creating a QT program to search for Customer Phone Number : Create a text file (customer.txt) and put names and p

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code

import sys
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QWidget, QLabel, QLineEdit
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtCore import QSize  

class MainWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        #dict for phonebook
        self.dictionary = {}
        #load data
        self.data()

        self.setMinimumSize(QSize(400, 140))  
        self.setWindowTitle("PhoneBook")

        self.nameLabel = QLabel(self)
        self.nameLabel.setText('Enter a name:')
        self.nameEdit = QLineEdit(self)
        self.nameEdit.move(120, 20)
        self.nameEdit.resize(200, 32)
        self.nameLabel.move(20, 20)

        pybutton = QPushButton('OK', self)
        pybutton.clicked.connect(self.clickMethod)
        pybutton.resize(200,32)
        pybutton.move(120, 60)      

        self.phoneLabel = QLabel(self)
        self.phoneLabel.setText('Phone Number:')
        self.phoneEdit = QLineEdit(self)
        self.phoneEdit.move(120,100)
        self.phoneEdit.resize(200, 32)
        self.phoneLabel.move(15, 100)

    def data(self):
        #open file
        f = open('phonebook.txt', 'r')
        lines = f.readlines()
        keys=[]
        values=[]
        #load keys and values
        for i,j in enumerate(lines):
            if i%2==0:
                keys.append(j.rstrip().lower())
            else:
                values.append(j.rstrip())
        #set data for further use
        self.dictionary = dict(zip(keys,values))

    def clickMethod(self):
        #get name
        name = self.nameEdit.text()
        #self.nameEdit.clear()
        #try to get number
        try :
            #if number exists set number
            number = self.dictionary[name]
            self.phoneEdit.setText(number)
        except KeyError:
            #else set Error text
            self.phoneEdit.setText("No Name exist!")

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    mainWin = MainWindow()
    mainWin.show()
    sys.exit( app.exec_() )

Screenshots

phonenumber.py X phonenumber.py >... import sys 2 from PyQt5 import QtCore, QtWidgets 3 from PyQt5.QtWidgets import QMainWind

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 def data(self): #open file open(phonebook.txt, r) lines f.readlines() keys=[

Outputs

Phone Book o x Enter a name: OK Phone Number

Phone Book ох Enter a name: jhon ок Phone Number 8543414221

Add a comment
Know the answer?
Add Answer to:
Exercise 3) Creating a QT program to search for Customer Phone Number : Create a text...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • How can I create Loops and Files on Java? • Create a program that reads a...

    How can I create Loops and Files on Java? • Create a program that reads a list of names from a source file and writes those names to a CSV file. The source file name and target CSV file name should be requested from the user • The source file can have a variable number of names so your program should be dynamic enough to read as many names as needed • When writing your CSV file, the first row...

  • Write a program **(IN C)** that displays all the phone numbers in a file that match the area code...

    Write a program **(IN C)** that displays all the phone numbers in a file that match the area code that the user is searching for. The program prompts the user to enter the phone number and the name of a file. The program writes the matching phone numbers to the output file. For example, Enter the file name: phone_numbers.txt Enter the area code: 813 Output: encoded words are written to file: 813_phone_numbers.txt The program reads the content of the file...

  • This program is used to create a phonebook for storing contact information into a text file....

    This program is used to create a phonebook for storing contact information into a text file. The program begins by asking the user to provide a name for the file that will contain their phone book information. It will then ask the user to provide the names and numbers of their contacts. It should keep asking the user for contact information until they type in Done (case-sensitive). After the user types Done, store all contact information into the file name...

  • Pig Latin Translator in Java The goal here is to read in the characters stored in a text file and create a second text file that contains the original text but with every word converted to “Pig-Latin....

    Pig Latin Translator in Java The goal here is to read in the characters stored in a text file and create a second text file that contains the original text but with every word converted to “Pig-Latin.” The rules used by Pig Latin are as follows: • If a word begins with a vowel, just as "yay" to the end. For example, "out" is translated into "outyay". • If it begins with a consonant, then we take all consonants before...

  • In C++, Write a program that retrieves a phone number from a txt files of names...

    In C++, Write a program that retrieves a phone number from a txt files of names and phone numbers. The program should ask the user to enter a name and then print the phone number of that person (or vice versa, number into name) The program should allow the user to repeat the operation as often as they would like to. The data is contained in two text files named "phoneNames.txt" and "phoneNums.txt". The files have one name or one...

  • Following should be done in C++: Create a program that reads a text file and prints...

    Following should be done in C++: Create a program that reads a text file and prints out the contents of the file but with all letters converted to uppercase. The name of the file to be read by the program will be provided by the user. Here are some example session: Contents of "data.txt": Hello, World! User input: data.txt Program output: HELLO, WORLD! Contents of "data.txt": tHiS FiLe HaS mIxEd CaSeS User input: data.txt Program output: THIS FILE HAS MIXED...

  • code creating

    Write a program that allows the user to navigate the lines of text in a file. The program should prompt the user for a filename and input the lines of text into a list. The program then enters a loop in which it prints the number of lines in the file and prompts the user for a line number. Actual line numbers range from 1 to the number of lines in the file. If the input is 0, the program...

  • Write a program in Java according to the following specifications: The program reads a text file...

    Write a program in Java according to the following specifications: The program reads a text file with student records (first name, last name and grade on each line). Then it prompts the user to enter a command, executes the command and loops. The commands are the following: "print" - prints the student records (first name, last name, grade). "sortfirst" - sorts the student records by first name. "sortlast" - sorts the student records by last name. "sortgrade" - sorts the...

  • Write a complete Python program with prompts for the user for the main text file (checks...

    Write a complete Python program with prompts for the user for the main text file (checks that it exists, and if not, output an error message and stop), for any possible flags (including none), and for any other input that this program may need from the user: split has an option of naming the smaller files head_tail list the first 10 lines (default) and the last 10 lines (default) in order of the given text file flag: -# output #...

  • In Java. Write a GUI contact list application. The program should allow you to input names...

    In Java. Write a GUI contact list application. The program should allow you to input names and phone numbers. You should also be able to input a name and have it display the previously entered phone number. The GUI should look something like the following, although you are welcome to format it in any way that works. This should be a GUI application with a JFrame. The program should contain two arrays of Strings. One array will contain a list...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT