Guest User

name generator

a guest
Jul 24th, 2025
9
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.55 KB | None | 0 0
  1. import sys
  2. import random
  3. from PyQt6.QtWidgets import (
  4. QApplication, QMainWindow, QWidget, QVBoxLayout,
  5. QPushButton, QLabel, QLineEdit, QHBoxLayout, QFrame
  6. )
  7. from PyQt6.QtCore import Qt, QPoint
  8. from PyQt6.QtGui import QColor, QIcon, QPixmap, QMouseEvent, QCursor, QPainter, QBrush
  9.  
  10.  
  11. AMERICAN_NAMES = [
  12. "James", "Mary", "John", "Patricia", "Robert", "Jennifer",
  13. "Michael", "Linda", "William", "Elizabeth", "David", "Barbara",
  14. "Richard", "Susan", "Joseph", "Jessica", "Thomas", "Sarah",
  15. "Charles", "Karen", "Christopher", "Nancy", "Daniel", "Lisa",
  16. "Matthew", "Betty", "Anthony", "Margaret", "Mark", "Sandra"
  17. ]
  18.  
  19. class CustomTitleBar(QFrame):
  20. def __init__(self, parent=None):
  21. super().__init__(parent)
  22. self.parent = parent
  23. self.setFixedHeight(40)
  24. self.setStyleSheet("""
  25. background-color: #2f80ed;
  26. color: white;
  27. """)
  28. self.layout = QHBoxLayout(self)
  29. self.layout.setContentsMargins(10, 0, 10, 0)
  30. self.layout.setSpacing(10)
  31.  
  32. self.title_label = QLabel("Random Name Generator")
  33. self.title_label.setStyleSheet("font-weight: bold; font-size: 14px;")
  34. self.layout.addWidget(self.title_label)
  35.  
  36. self.layout.addStretch()
  37.  
  38. self.min_btn = QPushButton("—")
  39. self.max_btn = QPushButton("❐")
  40. self.close_btn = QPushButton("✕")
  41.  
  42. for btn in (self.min_btn, self.max_btn, self.close_btn):
  43. btn.setFixedSize(30, 30)
  44. btn.setStyleSheet("""
  45. QPushButton {
  46. border: none;
  47. background-color: transparent;
  48. color: white;
  49. font-size: 16px;
  50. }
  51. QPushButton:hover {
  52. background-color: rgba(255, 255, 255, 0.2);
  53. }
  54. """)
  55. btn.setCursor(Qt.CursorShape.PointingHandCursor)
  56.  
  57. self.layout.addWidget(self.min_btn)
  58. self.layout.addWidget(self.max_btn)
  59. self.layout.addWidget(self.close_btn)
  60.  
  61. self.min_btn.clicked.connect(self.parent.showMinimized)
  62. self.max_btn.clicked.connect(self.toggle_max_restore)
  63. self.close_btn.clicked.connect(self.parent.close)
  64.  
  65. self._is_maximized = False
  66. self._start_pos = None
  67.  
  68. def toggle_max_restore(self):
  69. if self._is_maximized:
  70. self.parent.showNormal()
  71. self._is_maximized = False
  72. self.max_btn.setText("❐")
  73. else:
  74. self.parent.showMaximized()
  75. self._is_maximized = True
  76. self.max_btn.setText("❐")
  77.  
  78. def mousePressEvent(self, event: QMouseEvent):
  79. if event.button() == Qt.MouseButton.LeftButton:
  80. self._start_pos = event.globalPosition().toPoint()
  81. self._click_pos = self.mapToParent(event.position().toPoint())
  82. super().mousePressEvent(event)
  83.  
  84. def mouseMoveEvent(self, event: QMouseEvent):
  85. if self._start_pos:
  86. delta = event.globalPosition().toPoint() - self._start_pos
  87. new_pos = self.parent.pos() + delta
  88. self.parent.move(new_pos)
  89. self._start_pos = event.globalPosition().toPoint()
  90. super().mouseMoveEvent(event)
  91.  
  92. def mouseReleaseEvent(self, event: QMouseEvent):
  93. self._start_pos = None
  94. super().mouseReleaseEvent(event)
  95.  
  96.  
  97. class MainWindow(QMainWindow):
  98. def __init__(self):
  99. super().__init__()
  100. # Remove native title bar and frame
  101. self.setWindowFlags(Qt.WindowType.FramelessWindowHint | Qt.WindowType.Window)
  102. self.setMinimumSize(400, 250)
  103.  
  104. # Central widget and layout
  105. self.central_widget = QWidget()
  106. self.central_layout = QVBoxLayout(self.central_widget)
  107. self.central_layout.setContentsMargins(0, 0, 0, 0)
  108. self.central_layout.setSpacing(0)
  109. self.setCentralWidget(self.central_widget)
  110.  
  111. # Custom title bar
  112. self.title_bar = CustomTitleBar(self)
  113. self.central_layout.addWidget(self.title_bar)
  114.  
  115. # Main content container
  116. self.content = QWidget()
  117. self.content.setStyleSheet("background-color: #f4f6f8;")
  118. self.content_layout = QVBoxLayout(self.content)
  119. self.content_layout.setSpacing(15)
  120. self.content_layout.setContentsMargins(20, 20, 20, 20)
  121. self.central_layout.addWidget(self.content)
  122.  
  123. # Label
  124. self.label = QLabel("Random Name Generator")
  125. self.label.setAlignment(Qt.AlignmentFlag.AlignCenter)
  126. self.label.setStyleSheet("font-size: 16px; color: #333;")
  127. self.content_layout.addWidget(self.label)
  128.  
  129. # Input field
  130. self.input_field = QLineEdit()
  131. self.input_field.setFixedHeight(32)
  132. self.input_field.setPlaceholderText("Click the button to generate a name")
  133. self.input_field.setReadOnly(True)
  134. self.input_field.setStyleSheet("""
  135. QLineEdit {
  136. background-color: white;
  137. border: none;
  138. border-radius: 8px;
  139. padding: 0 10px;
  140. font-size: 14px;
  141. }
  142. """)
  143. self.add_shadow(self.input_field)
  144. self.content_layout.addWidget(self.input_field)
  145.  
  146. # Button
  147. self.button = QPushButton("Generate Name")
  148. self.button.setFixedHeight(36)
  149. self.button.setStyleSheet("""
  150. QPushButton {
  151. background-color: #4a90e2;
  152. color: white;
  153. border: none;
  154. border-radius: 8px;
  155. font-size: 14px;
  156. }
  157. QPushButton:hover {
  158. background-color: #357ABD;
  159. }
  160. QPushButton:pressed {
  161. background-color: #2C5DA3;
  162. }
  163. """)
  164. self.add_shadow(self.button)
  165. self.button.clicked.connect(self.generate_name)
  166. self.content_layout.addWidget(self.button)
  167.  
  168. def generate_name(self):
  169. name = random.choice(AMERICAN_NAMES)
  170. self.input_field.setText(name)
  171.  
  172. def add_shadow(self, widget):
  173. from PyQt6.QtWidgets import QGraphicsDropShadowEffect
  174. shadow = QGraphicsDropShadowEffect()
  175. shadow.setBlurRadius(15)
  176. shadow.setXOffset(0)
  177. shadow.setYOffset(3)
  178. shadow.setColor(QColor(0, 0, 0, 80))
  179. widget.setGraphicsEffect(shadow)
  180.  
  181.  
  182. def main():
  183. app = QApplication(sys.argv)
  184. window = MainWindow()
  185. window.show()
  186. sys.exit(app.exec())
  187.  
  188.  
  189. if __name__ == "__main__":
  190. main()
  191.  
Advertisement
Add Comment
Please, Sign In to add comment