Skip to content
The Chat Archive
gpt-4-0314· OpenAI WildChat-4.8M (AI2)

przekonwertuj ten kod na PyQt6: import customtkinter as ctk import keyboard class…

  1. User

    przekonwertuj ten kod na PyQt6:

    import customtkinter as ctk import keyboard

    class BindingEntry(ctk.CTkEntry): def init(self, root, focus_on=None, hotkey_function=None): ctk.CTkEntry.init(self, root, placeholder_text="") self.root = root self.focus_on = focus_on self.hotkey_function = hotkey_function self.key_combination = [] self.bind("", self.on_key_press) self.bind("", self.on_key_release) self.bind("", self.on_mouse_press)

    def on_key_press(self, event):
        separator = "_"
        if separator in event.keysym:
            first_part, second_part = event.keysym.split(separator, 1)
            new_string = separator.join([first_part])
        else:
            new_string = event.keysym
        if new_string not in self.key_combination:
            self.key_combination.append(new_string)
    
        self.configure(state="normal", border_color="orange")
        self.delete(0, 'end')
        self.insert(0, "+".join(self.key_combination))
        self.configure(state="readonly")
    
    def on_key_release(self, event):
        self.configure(border_color="grey")
        self.focus_on.focus_set()
        self.key_combination = []
        if self.hotkey_function is not None and self.get() != "":
            try:
                keyboard.clear_all_hotkeys()
                keyboard.add_hotkey(self.get(), self.hotkey_function, args=('From global keystroke',))
            except AttributeError:
                pass
    
    def on_mouse_press(self, event):
        self.configure(state="readonly", border_color="orange")
    
  2. Assistant

    import sys from PyQt6.QtCore import Qt from PyQt6.QtGui import QColor, QKeyEvent from PyQt6.QtWidgets import QApplication, QLineEdit, QWidget, QVBoxLayout

    import keyboard

    class BindingEntry(QLineEdit): def init(self, focus_on=None, hotkey_function=None): super().init() self.setPlaceholderText("") self.focus_on = focus_on self.hotkey_function = hotkey_function self.key_combination = [] self.setReadOnly(True)

    def keyPressEvent(self, event: QKeyEvent):
        separator = "_"
        key_name = QKeySequence(event.key()).name()
        if separator in key_name:
            first_part, second_part = key_name.split(separator, 1)
            new_string = separator.join([first_part])
        else:
            new_string = key_name
        if new_string not in self.key_combination:
            self.key_combination.append(new_string)
    
        self.setReadOnly(False)
        self.setText("+".join(self.key_combination))
        self.setReadOnly(True)
    
    def keyReleaseEvent(self, event: QKeyEvent):
        if self.focus_on is not None:
            self.focus_on.setFocus()
        self.key_combination = []
        if self.hotkey_function is not None and self.text() != "":
            try:
                keyboard.clear_all_hotkeys()
                keyboard.add_hotkey(self.text(), self.hotkey_function, args=('From global keystroke',))
            except AttributeError:
                pass
    
    def mousePressEvent(self, event):
        self.setReadOnly(True)
    

    if name == "main": app = QApplication(sys.argv)

    main_widget = QWidget()
    layout = QVBoxLayout(main_widget)
    
    entry = BindingEntry(focus_on=main_widget)
    layout.addWidget(entry)
    
    main_widget.show()
    sys.exit(app.exec())