Tkinter 主循环阻止了我的函数,电报机器人在我关闭 gui 之前不起作用



我的代码中的主要问题是root.mainloop((阻塞了所有其他函数,主要是与arduino通信并从队列中获取元素的函数。我一直在网上寻找这方面的信息,但我唯一发现的是创建一个新的线程,但不幸的是,这个线程不起作用。有人知道答案吗?无论如何,代码已经从Api和其他无用命令中清除我希望你能帮助我

import time
import telepot
from telepot.loop import MessageLoop
from telepot.namedtuple import ReplyKeyboardMarkup, KeyboardButton, InlineKeyboardMarkup, 
InlineKeyboardButton
from queuelib import FifoDiskQueue
from serial import Serial
import serial
from tkinter import  *
import tkinter.font as font
import threading

arduino = serial.Serial(port='COM4', baudrate=115200, timeout= .1)

q = FifoDiskQueue("NewOrdFile1")
s = FifoDiskQueue("Names")

keyboard = ReplyKeyboardMarkup(keyboard=[['🍹Order Drink', '⚙️help'], ['....', '....'],['🛎️about me', '🛎️Ping']])
inline_Key = InlineKeyboardMarkup(inline_keyboard=[
[InlineKeyboardButton(text="Margarita",callback_data='/Margarita'), InlineKeyboardButton(text="CocaCola",callback_data='/CocaCola')],
[InlineKeyboardButton(text="Coca-Buton",callback_data='/Coca-Buton') ,InlineKeyboardButton(text="Fanta",callback_data='/Fanta')]
]
)
keyboardDrinks = ReplyKeyboardMarkup(keyboard=[['🍹Margarita', '🍹Buton'], ['....', '....'],['🍹CocaCola', '🍹Fanta']])

root = Tk()
root.geometry("1920x1080")   
#set window color
root.configure(bg='grey')
myFont = font.Font(family='Helvetica', size=20, weight='bold')



def myClick():

label= Label(root, text= "YOU ORDERED A DRINK: Redbull-Vodka", bg= "white",padx= 20, pady=20)

label["font"]  = myFont
label.grid(row = 7, column = 540)

def myClick1():
label= Label(root, text= "YOU ORDERED A DRINK:       Margarita       ", bg="white",padx= 25, pady=20)
label["font"]  = myFont
label.grid(row = 7, column = 540)

def myClick2():
label= Label(root, text= "YOU ORDERED A DRINK:  coca-buton  ", bg= "white",padx= 30, pady=20)
label["font"]  = myFont
label.grid(row = 7, column = 540)

def handle(msg):
chat_id = msg['chat']['id'] # Receiving the message from telegram
command = msg['text']   # Getting text from the message
username = msg['from']['username']

b = bytes(username, 'utf-8')






Welcome = "Hi " +"@"+ username 
print ('Received:')
print(command)


if chat_id== or chat_id== -:

# Comparing the incoming message to send a reply according to it
if command == '🛎️about me':
bot.sendMessage (chat_id, str(Welcome)) 


if command == "ping":
bot.sendMessage(chat_id, str("pong"))
if command == "ding":
time.sleep(2)
bot.sendMessage(chat_id, str("dong"))
if command == "🛎️Ping":
bot.sendMessage(chat_id, str("Pong"))
if command == "Ding":
time.sleep(2)
bot.sendMessage(chat_id, str("Dong"))
if command == "⚙️help":
time.sleep(2)


if command == "/Start":
bot.sendMessage(chat_id, 'Hello', reply_markup=keyboard)

if command == "🍹Order Drink":
bot.sendMessage(chat_id, "Scegli drink qui sotto, usando la  keyboard", reply_markup=keyboardDrinks)


if command == "🍹Margarita":

bot.sendMessage(chat_id, "Your Margarita has been processed, Wait")
q.push(b"a")
#print(b("a"))
s.push(b)
print("Successfully pushed")
bot.sendMessage(chat_id, 'Redirecting', reply_markup=keyboard)



mylabel = Label(root, text= "SELF BARTENDER ROBOT", bg= "grey", padx= 540, pady= 100) 
mylabel["font"]  = myFont
mylabel2= Label(root, text= "name client", bg="grey", padx= 540, pady= 1)
mylabel2["font"]  = myFont
mylabel3 =Label(root, text= "Choose your cocktail", bg="grey", padx= 540, pady= 20)  
mylabel3["font"]  = myFont
mybutton= Button(root, text = "Redbull-Vodka",  command=  myClick , padx= 100, pady= 20 ,bg= "red") 
mybutton1= Button(root, text = "Margarita", command=  myClick1,padx= 100, pady= 20 , bg= "blue")
#mybutton.pack()
mybutton2= Button(root, text = "coca-buton",  command=myClick2,padx= 100, pady= 20 , bg= "yellow") 
#showing it on the screen

mylabel.grid(row = 1, column = 540 )
mylabel2.grid(row = 2, column = 540)
mylabel3.grid(row = 3, column = 540)
mybutton.grid(row = 4, column = 540)
mybutton1.grid(row =5, column = 540)
mybutton2.grid(row = 6, column = 540)


bot = telepot.Bot('')
print (bot.getMe())



# Start listening to the telegram bot and whenever a message is  received, the handle function will be called.


MessageLoop(bot, {'chat': handle}).run_as_thread()



def ReadList1():

#print(arduino.readline())
if arduino.readline()==b'1': 

print("ho inviato info!")
c= q.pop()
k = s.pop()
print(c)
print(k)
if c != None:

arduino.write(c)





root.mainloop()




print ('Listening....')

while 1:

ReadList1()

time.sleep(2)

```

我也遇到了同样的问题。我不确定是什么原因导致了这个错误。也许GUI运行的东西会干扰telegram bot,因为关闭GUI可以让它工作?不过我不确定。

最新更新