You are on page 1of 7

Question 1

import sys
from PyQt6.QtWidgets import *
from PyQt6.QtCore import *

class Calculator(QWidget):

def __init__(self):
super().__init__()

# Set up the user interface


grid = QGridLayout()
self.display = QLineEdit()
grid.addWidget(self.display, 0, 0, 1, 4)
self.display.setReadOnly(True)
self.display.setAlignment(Qt.AlignmentFlag.AlignRight)
self.display.setMaxLength(15)

font = self.display.font()
font.setPointSize(font.pointSize() + 8)
self.display.setFont(font)

self.add_buttons(grid)

self.setLayout(grid)
self.setWindowTitle("Calculator")

def add_buttons(self, grid):


names = ['7', '8', '9', '/',
'4', '5', '6', '*',
'1', '2', '3', '-',
'0', '💡', '=', '+']
positions = [(i, j) for i in range(1, 6) for j in range(4)]

for position, name in zip(positions, names):


button = QPushButton(name)
grid.addWidget(button, *position)
button.clicked.connect(self.button_clicked)

def button_clicked(self):
button = self.sender()
key = button.text()

if key == 'Close':
self.close()
elif key == '=':
result = eval(self.display.text())
self.display.setText(str(result))
elif key == 'Cls':
self.display.clear()
elif key == '💡':
self.showdialog()
else:
self.display.setText(self.display.text() + key)

def showdialog(self):
d = QDialog()
b1 = QPushButton("ok", d)
b1.clicked.connect(d.close)
b1.move(50, 50)
d.setWindowTitle("Dialog")
d.exec()

if __name__ == '__main__':
app = QApplication([])
calc = Calculator()
calc.show()
sys.exit(app.exec())

Question 2
from PyQt6.QtWidgets import *
from PyQt6.QtGui import *
import sys

class MyMainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
widget = QWidget()
menu_bar = self.menuBar()
file_menu = menu_bar.addMenu("File")
new_file = QAction("New", self)
open_file = QAction("View", self)
exit_file = QAction("Exit", self)
file_menu.addAction(new_file)
file_menu.addAction(open_file)
file_menu.addAction(exit_file)
new_file.triggered.connect(self.reset)
open_file.triggered.connect(self.read)
exit_file.triggered.connect(self.exit)
heading_name = QLabel("Survey")
heading_name.setFont(QFont("sans-serif"))
heading_name.setStyleSheet("font-size:32px;font-weight:bold;")
form_layout = QFormLayout()
self.name = QLineEdit()
self.roll = QLineEdit()
form_layout.addRow("Name: ", self.name)
form_layout.addRow("Roll# ", self.roll)
self.gender = QComboBox()
self.gender.setPlaceholderText("Choose")
self.gender.addItems(["Male", "Female", "Other"])
form_layout.addRow("Gender :", self.gender)
self.city = QComboBox()
self.city.setPlaceholderText("Choose")
self.city.addItems(["Gujrat", "Jalalpur Jattan", "Jehlum"])
form_layout.addRow("City :", self.city)
self.remarks = QTextEdit()
form_layout.addRow("Remarks:", self.remarks)
save_button = QPushButton("Save")
reset_button = QPushButton("Reset")
self.records = QLabel()
form_layout.addRow(save_button)
form_layout.addRow(reset_button)
form_layout.addRow(self.records)
reset_button.clicked.connect(self.reset)
save_button.clicked.connect(self.save)
widget.setLayout(form_layout)
self.setCentralWidget(widget)

def read(self):
file = open("records.txt", 'r')
with file:
records = file.read()
self.records.setText(records)

def reset(self):
self.name.clear()
self.roll.clear()
self.gender.setCurrentIndex(-1)
self.city.setCurrentIndex(-1)
self.remarks.clear()
self.records.clear()

def exit(self):
sys.exit()

def save(self):
file = open("records.txt", 'a')
data = self.name.text()+" "+self.roll.text()+"
"+self.gender.currentText()+" "+self.city.currentText()+"
"+self.remarks.toPlainText()+"\n"
with file:
file.write(data)

app = QApplication([])
window = MyMainWindow()
window.show()
sys.exit(app.exec())
Question 3
# 1
import random
def find_item_counts(t):
item_count = {}
for item in t:
if item in item_count:
item_count[item] += 1
else:
item_count[item] = 1

item_counts = [(item, count) for item, count in item_count.items()]


item_counts.sort()
return item_counts

# Create a tuple with random values


random_tuple = tuple(random.randint(0, 10) for _ in range(10))

# Find the count of each item in the tuple


item_counts = find_item_counts(random_tuple)

# Output the count of each item, sorted by item value in ascending order
print("The count of each item in the tuple is:")
for item, count in item_counts:
print(f"{item}->{count},")

# 2

list_ = [22.4,4.0,16.22,9.1,11.0,12.22,14.2,5.2,17.5]
list2_ = []
for i in list_:
i = int(i)
list2_.append(i)
print("Original list:", list_)
sorted_l2 = sorted(list2_)
print("Minimum Value:", sorted_l2[0])
print("Maximum Value:", sorted_l2[-1])
list3_ = []
for i in sorted_l2:
i *= 5
list3_.append(i)
print("Updated List Result:", list3_)
# 3

from itertools import product

def permutations_with_repetition(string, repetition_number):


characters = list(string)
permutations = [''.join(p) for p in product(characters,
repeat=repetition_number)]
return permutations

# Example usage
string = "xyz"
repetition_number = 3
permutations = permutations_with_repetition(string, repetition_number)
print("All permutations with repetition of", repetition_number, "characters
from string", string, ":")
for permutation in permutations:
print(permutation)

You might also like