Вот мой код:
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
# Define Form as a Class
class Form( QDialog):
# Form Constructor
def __init__(self, parent=None):
super(Form, self).__init__(parent)
cryptokey = 50
self.pbuttonName = QPushButton("Developer's Name")
self.lineeditName = QLineEdit("")
self.pbuttonSemester = QPushButton("Current Semester")
self.lineeditSemester = QLineEdit("")
self.pbutton0 = QPushButton("Open Input File")
self.lineedit0 = QLineEdit("Input File Name")
self.label1 = QLabel("Select a Function")
self.pbutton1 = QPushButton("Character Count")
self.lineedit1 = QLineEdit("")
self.pbutton2 = QPushButton("Line Count")
self.lineedit2 = QLineEdit("")
self.pbutton3 = QPushButton("Unique Word Count")
self.lineedit3 = QLineEdit("")
self.pbutton4 = QPushButton("Average Characters Per Word")
self.lineedit4 = QLineEdit("")
self.pbuttonQuit = QPushButton("Quit")
layout = QVBoxLayout()
layout.addWidget(self.pbuttonName)
layout.addWidget(self.lineeditName)
layout.addWidget(self.pbuttonSemester)
layout.addWidget(self.lineeditSemester)
layout.addWidget(self.pbutton0)
layout.addWidget(self.lineedit0)
layout.addWidget(self.lineedit0)
layout.addWidget(self.label1)
layout.addWidget(self.pbutton1)
layout.addWidget(self.lineedit1)
layout.addWidget(self.pbutton2)
layout.addWidget(self.lineedit2)
layout.addWidget(self.pbutton3)
layout.addWidget(self.lineedit3)
layout.addWidget(self.pbutton4)
layout.addWidget(self.lineedit4)
layout.addWidget(self.pbuttonQuit)
self.setLayout(layout)
ck = cryptokey
self.lineeditName.setFocus()
self.connect(self.pbuttonName, SIGNAL("clicked()"),self.buttonNamePressed)
self.connect(self.pbuttonSemester, SIGNAL("clicked()"),self.buttonSemesterPressed)
self.connect(self.pbutton0, SIGNAL("clicked()"),self.button0Pressed)
self.connect(self.pbutton1, SIGNAL("clicked()"),self.button1Pressed)
self.connect(self.pbutton2, SIGNAL("clicked()"),self.button2Pressed)
self.connect(self.pbutton3, SIGNAL("clicked()"),self.button3Pressed)
self.connect(self.pbutton4, SIGNAL("clicked()"),self.button4Pressed)
self.connect(self.pbuttonQuit, SIGNAL("clicked()"),self.buttonQuitPressed)
s = chr(ck + 20) + chr(ck) + chr(ck-2) +chr(ck-1)+chr(ck+6)
self.setWindowTitle(s)
# Form Methods
def buttonNamePressed(self):
self.lineeditName.setText("My Name is Roger Valladares")
def buttonSemesterPressed(self):
self.lineeditSemester.setText("Fall 2018")
def button0Pressed(self):
self.lineedit0.setText("FileOpened="+(name))
def button1Pressed(self):
self.lineedit1.setText("Characters in this file=" +str(numbercharacters))
def button2Pressed(self):
self.lineedit2.setText("Lines in this file=" + str(numberlines))
def button3Pressed(self):
self.lineedit3.setText("Words in this file=" + str(numberwords))
def button4Pressed(self):
self.lineedit4.setText("Average characters per word in this file=" +str(averagecharactersperword))
def buttonQuitPressed(self):
self.done(1)
app.quit()
# End of Form Class Definition
#this code below is executed based on the file opened.
d ={}
uniquewords=[]
numberlines=0
numberwords=0
numbercharacters=0
averagecharactersperword=0
uniqueword=0
x=raw_input ("Enter Filename:")
filehandle=open(x,'r')
try:
print ("This file has been opened=" + (x))
except:
print "File not found"
with filehandle as file:
for line in file:
line= line.lower()
line= line.strip()
line= line.strip('.')
line= line.strip('!')
line= line.strip('?')
line= line.replace(',','')
line= line.replace('-',' ')
words= line.split()
for ch in line:
if ch.isalpha()== True:
numbercharacters= numbercharacters + 1.0
numberlines = 1+ numberlines
numberwords += len(words)
averagecharactersperword = numbercharacters/numberwords
for word in words:
d[word]= d.get(word,0)+ 1
for word in d:
if word not in d:
uniqueword=0
if word in d:
uniqueword +=1
#print "Summary of file:"
#print ("1. Lines in this file=" + str(numberlines))
#print ("2. Words in this file=" + str(numberwords))
#print ("3. Characters in this file=" +str(numbercharacters))
#print ("4. Average characters per word in this file="
+str(averagecharactersperword))
#print ("5. Unique words, and their responding ocurrences in file="+
str(uniqueword))
Я новичок в Python.Таким образом, в основном пользователь вводит имя файла, которое он хочет, в поле self.lineedit0 = QLineEdit («Имя входного файла»), и мне нужно получить этот ввод в мой код, чтобы открыть указанный файл.