[英]Open another python file using tkinter button with parameters/entry
我正在嘗試為幣安構建一個交易機器人。 我已經設法構建了機器人並通過終端使用它。
但是,我想制作一個 GUI,它以與終端中相同的方式運行機器人,除了這次有一個界面。 我已經重建了機器人以連接到 binance 數據流以對其進行測試。 通過使用各種“代碼”(例如 ethusdt、btcusdt),我可以調整機器人以查看特定流以獲取價格信息。
現在的問題是我可以使用按鈕(鏈接到bot.py
)啟動程序,但是啟動后我仍然必須在終端中手動輸入代碼。
所以我的問題是 - 我如何將股票代碼作為參數傳遞給程序,以便它自動啟動,而不必在之后輸入它? 我基本上想在輸入字段(ticker_entry)中輸入一個代碼並將其傳遞給bot.py
代碼請求。
這是我為bot.py
編寫的代碼:
import websocket
import json
import pprint
from colorama import Fore, Back, Style
import numpy as np
import tkinter as tk
tmp = [1]
print("eg. ethusdt = ETH/USDT")
ticker = input(Fore.YELLOW + "Enter ticker here: ")
print(Style.RESET_ALL)
SOCKET="wss://stream.binance.com:9443/ws/"+ ticker +"@kline_1m"
def on_open(ws):
print('Connection established')
def on_close(ws):
print("Connection closed")
def on_message(ws, message):
global tmp
print("waiting for candle to close..")
json_message = json.loads(message)
candle = json_message['k']
is_candle_closed = candle['x']
close = candle['c']
if is_candle_closed:
print("\n")
print(Fore.RED + "Last candle closed at: ")
print(*tmp, sep= ' , ')
print(Style.RESET_ALL)
print("\n")
print(Fore.GREEN +"New candle closed at: \n{}".format(close))
print("\n")
print(Style.RESET_ALL)
tmp.pop(0)
tmp.append(float(close))
ws = websocket.WebSocketApp(SOCKET, on_open=on_open, on_close=on_close, on_message=on_message)
ws.run_forever()
這是我用tkinter
模塊編寫的代碼:
from tkinter import *
from tkinter import filedialog
import os
root = Tk()
WIDTH = 396
HEIGHT = 594
root.title('CheckBot')
root.geometry("396x594")
bg = PhotoImage(file = "rocket.png")
def start_bot(ticker_entry):
trader = 'bot.py'
os.system('"%s"' % trader)
#Creating canvas
my_canvas = Canvas(root, width=WIDTH, height=HEIGHT)
my_canvas.pack(fill="both", expand=True)
#Setting image in canvas
my_canvas.create_image(0,0, image=bg, anchor="nw")
#Adding a label
my_canvas.create_text(200,30, text="Enter the ticker to trade", font=("Bembo Bold Italic", 15), fill="#cc5200")
ticker_entry = Entry(my_canvas, bg='white')
ticker.pack(pady=50)
my_canvas.create_text(200,100, text="Enter amount to trade", font=("Bembo Bold Italic", 15), fill="#cc5200")
amount = Entry(my_canvas, bg='white')
amount.pack(pady=10)
trade_button = Button(my_canvas, text='Start trading', bg='#00b359', fg='#000099', command=lambda:start_bot(ticker))
trade_button.pack(pady=70)
root.mainloop()
Generally, it isn't the best practice to spawn a whole new CMD shell using os.system()
, and it is better to put all the main functionality in a function, then import that function and call it.
而不是在tkinter
文件中寫這個:
def start_bot(ticker_entry):
trader = 'bot.py'
os.system('"%s"' % trader)
將bot.py
的邏輯放入單個 function 中,如下所示:
def start_bot(ticker):
print("eg. ethusdt = ETH/USDT")
print(Style.RESET_ALL)
SOCKET="wss://stream.binance.com:9443/ws/"+ ticker +"@kline_1m"
ws = websocket.WebSocketApp(SOCKET, on_open=on_open, on_close=on_close, on_message=on_message)
ws.run_forever()
然后,回到tkinter
文件,您可以簡單地使用from bot import start_bot
,並且邏輯應該保持完整。
但是,如果您不想更改代碼的任何主要方面,並且想繼續使用os.system()
,那么您的問題還有另一種解決方案(同樣,強烈建議使用上述解決方案,因為您不依賴於多余的os.system()
調用,這會浪費更多的 CPU 和 RAM 資源)。
運行文件時,可以在命令行中指定文件 arguments。 這些 arguments 被傳遞給腳本,但如果不使用它們就不會做任何事情。 但是,在您的情況下,由於您想訪問它們,您可以這樣做:
from sys import argv
# Get the first command line argument
first_argument = argv[1]
# The thing to notice here is that you start from the index 1, since 0 is the script name. (When you run a python script, you are calling the python.exe file, and the script name is the first argument)
這與您的問題有何聯系? 好吧,既然您已經在命令行中運行了bot.py
文件(使用os.system()
),您可以添加一個命令行參數來指定代碼。 然后,從bot.py
中,您可以從sys.argv
中獲取該股票代碼值。
def start_bot(ticker_entry):
trader = 'bot.py'
os.system('"{}" "{}"'.format(trader, ticker_entry)) # Pass the ticker after the script name
在bot.py
文件中...
from sys import argv
ticker = argv[1]
您可以將其作為命令行參數傳遞。
import sys
ticker = ""
if len(sys.argv) > 1:
ticker = sys.argv[1]
然后,如果ticker
不為空,只需填寫文本框,並自己調用start_bot
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.