Редактирование наложений и границ для классического блокнота WindowsPython

Программы на Python
Ответить Пред. темаСлед. тема
Anonymous
 Редактирование наложений и границ для классического блокнота Windows

Сообщение Anonymous »

сделал клон окон в блокноте и не могу сделать так, чтобы граница вкладок совпадала с границей строки меню

Код: Выделить всё

from PyQt6.QtCore import Qt
from PyQt6.QtGui import QIcon, QAction
from PyQt6.QtWidgets import QApplication, QWidget, QVBoxLayout, QTextEdit, QFileDialog, QMenuBar, QPushButton, QLabel, QHBoxLayout, QStackedWidget

class Notepad(QWidget):
def __init__(self):
super().__init__()

self.setWindowTitle("Notepad with Tabs")
self.setGeometry(100, 100, 800, 600)
self.setWindowIcon(QIcon("icon.png"))  # Ensure you have the correct icon.png file

# Remove default title bar
self.setWindowFlags(Qt.WindowType.FramelessWindowHint)

# Main layout
self.main_layout = QVBoxLayout(self)
self.main_layout.setContentsMargins(0, 0, 0, 0)

# Create the custom title bar with file menu
self.create_custom_title_bar()

# Create a stacked widget to manage content for each "tab"
self.stacked_widget = QStackedWidget()
self.main_layout.addWidget(self.stacked_widget)

# Initialize the first tab
self.new_file()

def create_custom_title_bar(self):
# Custom title bar layout
title_bar_layout = QVBoxLayout()

# Title bar top section
title_bar_top = QHBoxLayout()

# Add the application icon to the title bar (left side)
self.icon_button = QLabel(self)
self.icon_button.setPixmap(QIcon("icon.png").pixmap(16, 16))  # Ensure you have the correct icon.png file
title_bar_top.addWidget(self.icon_button)

# Add the window title
self.window_title = QLabel("Notepad", self)
self.window_title.setStyleSheet("color: white; font-size: 12px;")
title_bar_top.addWidget(self.window_title)

# Add a container for tab buttons (title bar tabs)
self.tab_buttons_container = QHBoxLayout()
title_bar_top.addLayout(self.tab_buttons_container)

# Add a "New Tab" button to the right of the tab names
self.new_tab_button = QPushButton("+", self)
self.new_tab_button.clicked.connect(self.new_file)
self.new_tab_button.setStyleSheet("background-color: transparent; border: none; color: white; font-size: 18px;")
title_bar_top.addWidget(self.new_tab_button)

# Add a spacer to push minimize, maximize buttons to the right
title_bar_top.addStretch()

# Minimize button (right side)
self.minimize_button = QPushButton("_", self)
self.minimize_button.clicked.connect(self.showMinimized)
self.minimize_button.setFixedSize(25, 25)  # Set fixed size
self.minimize_button.setStyleSheet("background-color: transparent; border: none; color: white; font-size: 12px;")
title_bar_top.addWidget(self.minimize_button)

# Maximize button (right side)
self.maximize_button = QPushButton("⬜", self)
self.maximize_button.clicked.connect(self.showMaximized)
self.maximize_button.setFixedSize(25, 25)  # Set fixed size
self.maximize_button.setStyleSheet("background-color: transparent; border: none; color: white; font-size: 12px;")
title_bar_top.addWidget(self.maximize_button)

# Close button (right side)
self.close_button = QPushButton("X", self)
self.close_button.clicked.connect(self.close)
self.close_button.setFixedSize(25, 25)  # Set fixed size
self.close_button.setStyleSheet("background-color: transparent; border: none; color: white; font-size: 12px;")
title_bar_top.addWidget(self.close_button)

title_bar_layout.addLayout(title_bar_top)

# Set the custom title bar layout to the window's layout
self.main_layout.addLayout(title_bar_layout)

def create_file_menu_bar(self, parent_widget):
# Create a custom file menu bar
menu_bar = QMenuBar(parent_widget)
menu_bar.setStyleSheet("""
background-color: #333333;
color: white;
border-bottom: 2px solid #444444;
padding: 5px;
font-size: 10px;   # Lower font size for the menu items
""")

# Create File menu
file_menu = menu_bar.addMenu("File")

# Add actions to the File menu
new_action = QAction("New", parent_widget)
new_action.triggered.connect(self.new_file)
file_menu.addAction(new_action)

open_action = QAction("Open", parent_widget)
open_action.triggered.connect(self.open_file)
file_menu.addAction(open_action)

return menu_bar

def new_file(self):
# Create a new tab with a text editor
tab_content_widget = QWidget()
tab_layout = QVBoxLayout(tab_content_widget)

# Create the custom file menu bar and add it to the layout
menu_bar = self.create_file_menu_bar(tab_content_widget)
tab_layout.addWidget(menu_bar)

# Create the text editor and add it to the layout
text_edit = QTextEdit(self)
text_edit.setStyleSheet("""
background-color: #222222;
color: white;
font-family: Arial;
font-size: 12px;
border: 2px solid #444444;
border-radius: 5px;
""")
tab_layout.addWidget(text_edit)

# Apply border around the entire tab content (including menu bar and text editor)
tab_content_widget.setStyleSheet("""
border: 3px solid #444444;
border-radius: 5px;
padding: 5px;
""")

# Add this content to the stacked widget
self.stacked_widget.addWidget(tab_content_widget)

# Create a tab button for the title bar
self.create_tab_button(tab_content_widget)

def open_file(self):
# Open a file dialog to select a file to open
file_path, _ = QFileDialog.getOpenFileName(self, "Open File", "", "Text Files (*.txt);;All Files (*)")
if file_path:
with open(file_path, 'r') as file:
content = file.read()

# Create new tab and add content
tab_content_widget = QWidget()
tab_layout = QVBoxLayout(tab_content_widget)

# Create the custom file menu bar and add it to the layout
menu_bar = self.create_file_menu_bar(tab_content_widget)
tab_layout.addWidget(menu_bar)

# Create the text editor, set content, and add it to the layout
text_edit = QTextEdit(self)
text_edit.setText(content)
text_edit.setStyleSheet("""
background-color: #222222;
color: white;
font-family: Arial;
font-size: 12px;
border: 2px solid #444444;
border-radius: 5px;
""")
tab_layout.addWidget(text_edit)

# Apply border around the entire tab content (including menu bar and text editor)
tab_content_widget.setStyleSheet("""
border: 3px solid #444444;
border-radius: 5px;
padding: 5px;
""")

# Add this content to the stacked widget
self.stacked_widget.addWidget(tab_content_widget)

# Create a tab button for the title bar
self.create_tab_button(tab_content_widget)

def create_tab_button(self, tab_content_widget):
# Create a button for the new tab in the title bar
tab_button = QPushButton(f"Tab {self.stacked_widget.count()}", self)
tab_button.setStyleSheet("""
background-color: transparent;
color: white;
border: 1px solid #444444;
padding: 5px;
font-size: 14px;
""")
tab_button.clicked.connect(lambda:  self.select_tab(tab_content_widget))
self.tab_buttons_container.addWidget(tab_button)

def select_tab(self, tab_content_widget):
# Switch the active tab by changing the displayed widget in the stacked widget
index = self.stacked_widget.indexOf(tab_content_widget)
self.stacked_widget.setCurrentIndex(index)

# Initialize the application
app = QApplication(sys.argv)
window = Notepad()
window.show()
sys.exit(app.exec())

как получить границы, как в блокноте Windows
[img]https://i.sstatic. net/2m24c2M6.png[/img]

Изображение

как мне сделать границы равными одной по сравнению с окнами, где вкладка и раскрывающийся список файлов в строке меню представляют собой одну открытую границу. мне сделать это из меню, потому что это не работает

Подробнее здесь: https://stackoverflow.com/questions/792 ... ws-notepad
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «Python»