簡體   English   中英

Tkinter GUI 未顯示,但功能收到響應

[英]Tkinter GUI is not showing, But Response is received for the functions

Im trying to create GUI using tkinter, objective is to call a function from Button(Tkinter Window), without websocket function, i'm able to get Tkinter window and able to execute the function via button, trying the same with Websocket client function,首先執行功能,並且 Tkinter window 未顯示 UP。

from tkinter import *
from threading import *
import websocket
import threading
window=Tk()
window.title("Scalp")
window.geometry('400x400')
window.config(bg='lavender')


def Login():
    import requests
    import json
    global tokens

    headers = {
         'Content-Type': 'application/json',
          'Accept': 'application/json'
        }
    url = 'https://api.stocknote.com/login'
    myobj = { 'userId': 'DDXX12', 'password': 'Durant', 'yob': '1999'}

    res = requests.post(url, data = json.dumps(myobj), headers = headers)
    tokens = res.json().get("sessionToken")
    print (res.text);
    print(tokens)

def on_message(ws, msg):
        print ("Message Arrived:" + msg)
        return
def on_error(ws, error):
        print (error)
        return
def on_close(ws):
        print ("Connection Closed")
        return
def on_open(ws):
        print ("Sending json")
        data='{"request":{"streaming_type":"quote", "data":{"symbols":[{"symbol":"45402_NFO"}]}, "request_type":"subscribe", "response_format":"json"}}'
        ws.send(data)
        ws.send("\n")
        return
def connection():
        Login()
        headers = {'x-session-token': tokens }
        websocket.enableTrace(True)
        ws = websocket.WebSocketApp("wss://stream.stocknote.com", on_open = on_open, on_message = on_message, on_error = on_error, on_close = on_close, header = headers)
        ws.run_forever()
        return
a=Button(window,text='Login',width=12,bg='azure',command=Login(),fg='black',font('bold',14),activebackground='dark sea green',activeforeground='khaki3')
a.place(x=100,y=50)
b=Button(window,text='BankNifty',width=12,bg='azure',command=connection(),fg='black',font('bold',14),activebackground='dark sea green',activeforeground='khaki3')
b.place(x=100,y=90)
window.mainloop

Tkinter window 沒有出現,而是直接執行函數。

您的代碼中有幾個問題。

首先,您沒有調用mainloop 您需要將window.mainloop更改為window.mainloop()

第二個問題是您在啟動時立即調用connectionLogin ,因為您指定它們錯誤。

創建按鈕時,您必須傳遞一個callable 在您的情況下,您立即調用 function 然后將結果傳遞給command選項。 您需要將command=Login()更改為command=Login ,並將command=connection()更改為command=connection

最后,當調用connection時,它調用ws.run_forever() 這是一個阻塞調用——它不會返回。 因為它沒有返回,所以 tkinter 事件循環(從mainloop()開始)永遠沒有機會運行。 因為它不運行,所以它甚至無法處理基本事件,例如顯示 window 的請求。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM