Answer the question
In order to leave comments, you need to log in
How to close the window and start the bot by clicking on the button?
You need to make sure that tkinter closes completely and only the while loop remains, which would be responsible for the bot
import os
import vk_api
import time
import random
from tkinter import *
from tkinter import messagebox
textuser = "Текст пользователя"
textbot = "Текст бота"
root = Tk()
root.title("MyVKBot 1.2")
root.geometry("600x600")
messagebox.showinfo("Важное сообщение",message="Текст пользователя задавать маленькими буквами!\nПример - привет (пользователь) Привет :3 (бот)")
def start_bot():
global root
root.destroy()
token = API
vk = vk_api.VkApi(token=API)
vk._auth_token()
while True:
try:
messages = vk.method("messages.getConversations", {"offset": 0, "count": 20, "filter": "unanswered"})
if messages["count"] >= 1:
id = messages["items"][0]["last_message"]["from_id"]
body = messages["items"][0]["last_message"]["text"]
if body.lower() == mes_user1:
vk.method("messages.send", {"peer_id": id, "message": mes_bot1, "random_id": random.randint(1, 2147483647)})
elif body.lower() == mes_user2:
vk.method("messages.send", {"peer_id": id, "message": mes_bot2, "random_id": random.randint(1, 2147483647)})
except Exception as E:
time.sleep(1)
num1 = StringVar()
num1_label = Label(font="Arial 18",text="1")
num1_label.place(relx=.6, rely=.1, anchor="c")
mes_user1 = Entry()
mes_user1.insert(0,textuser)
mes_user1.place(relx=.1, rely=.1, anchor="c")
mes_bot1 = Entry()
mes_bot1.insert(0,textbot)
mes_bot1.place(relx=.4, rely=.1, anchor="c")
num2 = StringVar()
num2_label = Label(font="Arial 18",text="2")
num2_label.place(relx=.6, rely=.2, anchor="c")
mes_user2 = Entry()
mes_user2.insert(0,textuser)
mes_user2.place(relx=.1, rely=.2, anchor="c")
mes_bot2 = Entry()
mes_bot2.insert(0,textbot)
mes_bot2.place(relx=.4, rely=.2, anchor="c")
num3 = StringVar()
num3_label = Label(font="Arial 18",text="3")
num3_label.place(relx=.6, rely=.3, anchor="c")
mes_user3 = Entry()
mes_user3.insert(0,textuser)
mes_user3.place(relx=.1, rely=.3, anchor="c")
mes_bot3 = Entry()
mes_bot3.insert(0,textbot)
mes_bot3.place(relx=.4, rely=.3, anchor="c")
num4 = StringVar()
num4_label = Label(font="Arial 18",text="4")
num4_label.place(relx=.6, rely=.4, anchor="c")
mes_user4 = Entry()
mes_user4.insert(0,textuser)
mes_user4.place(relx=.1, rely=.4, anchor="c")
mes_bot4 = Entry()
mes_bot4.insert(0,textbot)
mes_bot4.place(relx=.4, rely=.4, anchor="c")
num5 = StringVar()
num5_label = Label(font="Arial 18",text="5")
num5_label.place(relx=.6, rely=.5, anchor="c")
mes_user5 = Entry()
mes_user5.insert(0,textuser)
mes_user5.place(relx=.1, rely=.5, anchor="c")
mes_bot5 = Entry()
mes_bot5.insert(0,textbot)
mes_bot5.place(relx=.4, rely=.5, anchor="c")
num6= StringVar()
num6_label = Label(font="Arial 18",text="6")
num6_label.place(relx=.6, rely=.6, anchor="c")
mes_user6 = Entry()
mes_user6.insert(0,textuser)
mes_user6.place(relx=.1, rely=.6, anchor="c")
mes_bot6 = Entry()
mes_bot6.insert(0,textbot)
mes_bot6.place(relx=.4, rely=.6, anchor="c")
API = StringVar()
API_label = Label(font="Arial 14",text="Введите ключ API")
API_label.place(relx=.2, rely=.7, anchor="c")
APIkey = Entry()
APIkey.place(relx=.5, rely=.7, anchor="c")
message_button = Button(text="Запустить бота", command=start_bot,background="#000000",foreground="#ffffff",font="Arial 15")
message_button.place(relx=.8, rely=.7, anchor="c")
info = StringVar()
info_label = Label(font="Arial 10",text="Обязательно заполните все поля,если хоть 1 строчка будет пустая бот не запустится!\nКлюч API это уникальный ключ вашего сообщества который предоставляет\n доступ программе MyVKBot")
info_label.place(relx=.5, rely=.8, anchor="c")
root.mainloop()
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question