#๐Ÿ”’ PyQt5 Error #pyqt5

113 messages ยท Page 1 of 1 (latest)

wild nexusBOT
#

@sudden scarab

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

sudden scarab
#
class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle(' ')
        self.setGeometry(100, 100, 1050, 700)
        self.browser = QWebEngineView()
        self.browser.setHtml(html_content)
        self.setCentralWidget(self.browser)
        self.channel = QWebChannel()
        self.browser.page().setWebChannel(self.channel)
        self.channel.registerObject("bridge", self)

class AuthWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.data = None
        self.password = None
        self.license_key = None
        self.username = None

        self.setWindowTitle(' ')
        self.setGeometry(100, 100, 1050, 700)
        self.browser = QWebEngineView()
        self.browser.setHtml(auth_content)
        self.setCentralWidget(self.browser)
        self.channel = QWebChannel()
        self.browser.page().setWebChannel(self.channel)
        self.channel.registerObject("bridge", self)

    def login(self):
        print(self.username, self.password)
        try:
            keyauthapp.login(self.username, self.password)
            print("logged in")

            response = jsonify({"message": "success"})
            response.status_code = 200

            w = MainWindow()
            w.show()
            self.close()
        except Exception as e:
            print(f"{e}")
            return jsonify({"error": str(e)}), 400

    def register(self):
        print(self.username, self.password, self.license_key)
        try:
            keyauthapp.register(self.username, self.password, self.license_key)

            response = jsonify({"message": "success"})
            response.status_code = 200

            w = MainWindow()
            w.show()
            self.close()
        except Exception as e:
            print(f"{e}")
            return jsonify({"error": str(e)}), 400

@app.route('/auth', methods=['POST'])
@cross_origin()
def auth():
    data = request.json
    username = data.get('username')
    password = data.get('password')
    license_key = data.get('license_key')

    if not username or not password:
        return jsonify({"error": "error"}), 400

    window = AuthWindow()
    window.username = username
    window.password = password
    window.license_key = license_key

    if license_key:
        return window.register()
    else:
        return window.login()

if __name__ == '__main__':
    system("cls")
    CORS(app)
    flask_thread = Thread(target=lambda: app.run(port=5000, debug=False, use_reloader=False)).start()

    app = QApplication(sys.argv)
    window = AuthWindow()
    window.setFixedSize(800, 650)
    window.setWindowFlags(Qt.Window | Qt.WindowStaysOnTopHint)
    window.setAttribute(Qt.WA_TranslucentBackground)

    hook_thread = threading.Thread(target=keyboard_hook, daemon=True).start()
    set_dark_mode(int(window.winId()))

    window.show()
    exit(app.exec_())
#

QObject::startTimer: Timers can only be used with threads started with QThread

foggy cypress
foggy cypress
#

Sure, you're importing it. But the error complains about it being started. Are you starting a QTimer somewhere or do you have an idea of what could be?

sudden scarab
#

it's gray, i mean QTimer is not being used anywhere in my code

foggy cypress
sudden scarab
#
def keyboard_hook():
    global script_active
    while True:
        if GetAsyncKeyState(ready_toggle_key):
            toggle_script()
            time.sleep(0.3)
foggy cypress
#

Alright, GetAsyncKeyState seems to be a WinAPI function so probably not that. Maybe toggle_script and whatever that's calling?

#

!paste if you can share the entire code, that would work as well

wild nexusBOT
#
Pasting large amounts of code

If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/

After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.

sudden scarab
#

i can't, sorry

def toggle_script():
    global script_active
    script_active = not script_active

    if script_active:
        start_recoil_control()

    return "enabled" if script_active else "disabled"
#

in start_recoil_control all the same:

        if is_authenticated:
            while script_active:
                if GetAsyncKeyState(0x01) & 0x8000:

and it's can be called after authentication, problem in authentication so basically it's not starting and error is not here

#
hook_thread = threading.Thread(target=keyboard_hook, daemon=True).start()

removed and this error still appears

foggy cypress
sudden scarab
# sudden scarab

i have no any timers in my code gray means it's not used anywhere in my full code

#

error exactly here:

            w = MainWindow()
            w.show()

but idk how to fix it

foggy cypress
# sudden scarab error exactly here: ```python w = MainWindow() w.show() ...

Ah, I see what's happening.

You can't start a window like that since it's not within Qt's own event loop.
Since your route is effectively invoked by Flask using its own threads, Qt can't really start a window there since that needs to be done within the event loop.

There's probably workarounds for fixing this but I'll suggest just splitting the Flask code and the Qt code into separate modules then have your auth window send the requests to Flask and have Flask start a new process with your actual application.
-# There's also Qt Http Server as an alternative to Flask but, long-term, you'd still want to do these things separately.

eager adder
#

Yeah my suggestion in python-discussion was to have them be separate processes too

sudden scarab
eager adder
sudden scarab
#

yeah but i need like to create second file(showing src) and starting it

eager adder
#

So what exactly is the relationship between your gui and the flask server?

#

i'm still not quite understanding what you're doing

sudden scarab
#
@app.route('/update_settings', methods=['POST'])
def update_settings_route():
    global script_active, toggle_key, crouch_key, selected_weapon, selected_scope, x_speed, y_speed, menu_key, ready_crouch_key, ready_toggle_key
    data = request.json
    print(data)

    if data.get('id') == "toggle_key":
        toggle_key = data.get('value')
    if toggle_key == "F2":
        toggle_key = 0x71
        ready_toggle_key = toggle_key
    if toggle_key == "F3":
        toggle_key = 0x7
    if toggle_key == "F5":
        toggle_key = 0x74
        ready_toggle_key = toggle_key
    if toggle_key == "F6":
        toggle_key = 0x75
        ready_toggle_key = toggle_key
    if toggle_key == "F7":
        toggle_key = 0x76
        ready_toggle_key = toggle_key
    if toggle_key == "F8":
        toggle_key = 0x77
        ready_toggle_key = toggle_key
    elif data.get('id') == "crouch_key":
        crouch_key = data.get('value')
    if crouch_key == "Ctrl":
        crouch_key = 0x11
    if crouch_key == "Shift":
        crouch_key = 0x10
        ready_crouch_key = crouch_key
    elif crouch_key == "Alt":
        crouch_key = 0x12
        ready_crouch_key = crouch_key
    elif crouch_key == "C":
        crouch_key = 0x43
        ready_crouch_key = crouch_key
    elif data.get('id') == "selected_weapon":
        selected_weapon = data.get('value')
    elif data.get('id') == "selected_scope":
        selected_scope = data.get('value')
        update_scope(selected_scope)
    # elif data.get('id') == "menu_key":
    #     menu_key = data.get('value')
    #     if menu_key == "Insert":
    #         menu_key = keyboard.Key.insert
    #     elif menu_key == "Home":
    #         menu_key = keyboard.Key.home
    elif data.get('id') == "x_speed":
        x_speed = data.get('value')
        x_speed_int = int(x_speed)
        update_x_speed(x_speed_int)
    elif data.get('id') == "y_speed":
        y_speed = data.get('value')
        y_speed_int = int(y_speed)
        update_y_speed(y_speed_int)
#

script for game, updating user settings

sudden scarab
eager adder
#

Why do you need flask for this?

sudden scarab
#

what can i use else?

eager adder
#

Can you accomplish this with just a Qt gui?

sudden scarab
#

i dont think that qss is better than css

#

im trying to make good gui

eager adder
#

that's accessable via a web browser? If that's so then you don't need Qt - just use chrome or whatever browser you want

foggy cypress
eager adder
#

Yeah there's no reason to bring network communication protocols in to a completely localized gui

sudden scarab
eager adder
#

Just implement your gui in Qt

sudden scarab
#

sorry, my english is not so good. implement html and css in qt?

eager adder
#

no. If you're not making a website, you don't need html and css to begin with

#

your gui can be done completely with Qt framework

sudden scarab
#

yeah but it's already done on html&css

eager adder
#

Ok. Then serve it up with flask and use chrome/safari/firefox/edge to view it. No qt needed then

sudden scarab
#

this code is starting script in your pc, i can't do that

eager adder
#

why not?

sudden scarab
#

?

sudden scarab
#

or you mean starting flask locally

eager adder
#

Ok just looking at the code you shared at the beginning here

#

So who is making the POST to your auth route?

sudden scarab
eager adder
sudden scarab
#

from web

eager adder
#

from this?

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle(' ')
        self.setGeometry(100, 100, 1050, 700)
        self.browser = QWebEngineView()
        self.browser.setHtml(html_content)
        self.setCentralWidget(self.browser)
        self.channel = QWebChannel()
        self.browser.page().setWebChannel(self.channel)
        self.channel.registerObject("bridge", self)

Does that html_content have the js that sends a POST to your auth route?

sudden scarab
#

no it's other window

#

not auth

eager adder
#

ok, but auth_content has the HTML that POST's to your auth route?

sudden scarab
#
const response = await fetch('http://localhost:5000/auth', {
#

yes

eager adder
#

ok

#

so you're trying to implement both a browser/client and a server in the same process

sudden scarab
#

i did it before

#

yep

eager adder
#

the flask server should actually serve that html content as another route

#

so you use a client process (like chrome for example) to go to http://localhost:5000/login or whatever you call it

#

that login route will serve the HTML

#

which contains the JS to POST to /auth

#

you don't need Qt to create a browser window

sudden scarab
#

it's not my decision

eager adder
sudden scarab
#

why not anyway it works

eager adder
#

just do everything in qt

sudden scarab
eager adder
#

๐Ÿคท i tried man

#

do what you want

sudden scarab
#

i understand what you mean but i dont know qss so good

eager adder
#

you don't strictly need qss

#

it can all be programmed directly

sudden scarab
#

directly? what you mean

foggy cypress
eager adder
#

You just have a window with a couple of text entries right?

sudden scarab
#

huh? no

#

i'm not doing that because i used qtwebbrowser

#

even forget about qt creator

eager adder
#

What exactly are your requirements for this project?

sudden scarab
#

requirements?

#

wdym

eager adder
#

30,000ft view of what you're trying to accomplish

#

Sounds like some other guy is giving you requirements. Like "make a better gui"

#

what does that mean

sudden scarab
#

macros for recoil control in game

eager adder
#

Ok. And you're trying to create a Gui to configure it?

sudden scarab
sudden scarab
eager adder
#

I still don't think you need both. In my opinion, you should use one or the other

#

Either serve everything in flask and use a browser to view it, or create the entire gui in Qt

sudden scarab
#

i have no time actually

#

to create a new one gui

eager adder
#

Sometimes you gotta take a step back to move forward

#

it's okay

sudden scarab
#

problem is not in me

#

i should release a new update today

#

thats why

eager adder
#

๐Ÿคท not my problem. I'm just speaking from experience here

#

I've given you my suggestions. It's up to you do take it or leave it

sudden scarab
#

yeah i appreciate it ty

#

ill try split codes

wild nexusBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.