Этот вопрос закрыт для ответов, так как повторяет вопрос Как очистить поле для ввода текста в tkinter?
@Xyyax1337

Как правильно передать значение из виджета Entry в tkinter?

создал GUI на tkinter, на второй вкладке такой функционал - кнопка подключиться к бд, поле для ввода запросов и кнопка чтобы отправить запрос из поля

from email import message
from multiprocessing import connection
from sqlite3 import connect
from tkinter import *  
from tkinter import ttk  
import pymysql
from config import host,user,password,db_name


def text_open():
    text.delete("1.0","end")  
    file = open(r"C:\wamp64\bin\mysql\mysql8.0.29\data\mysql-slow.log").read()
    text.insert(0.0, file)
    text.see("end")  

def truncate_file():
    file = open(r"C:\wamp64\bin\mysql\mysql8.0.29\data\mysql-slow.log","w")
    file.truncate(0) 
    text.delete('1.0', END)
    
def update_file():
    text.delete("1.0","end")  
    file = open(r"C:\wamp64\bin\mysql\mysql8.0.29\data\mysql-slow.log").read()
    text.insert(0.0, file)
    text.see("end")  

def bd_connect():
 try:
     global connection
     connection = pymysql.connect(
     host = host,
     port = 3306,
     user = user,
     password = password,
     database= db_name,
     cursorclass=pymysql.cursors.DictCursor
    )    
     print("Подключение успешно")
 except Exception as ex:
    print("Соединение сброшено")    
    print(ex)
 
 
def bd_send():
     try:
         with connection.cursor() as cursor:
             send_sql = entry_code.get()
             cursor.execute(send_sql)
             connection.commit()
     finally:
         connection.close()    
    

  
window = Tk()  
window.title("Программа для курсовой работы")  
window.geometry('850x700')  
tab_control = ttk.Notebook(window)  
tab1 = ttk.Frame(tab_control)  
tab2 = ttk.Frame(tab_control)  
tab_control.add(tab1, text='Просмотр Лога')  
tab_control.add(tab2, text='Изменения в MySQL')  
tab_control.pack(expand=1, fill='both')  

#Первая вкладка
btn1 = Button(tab1,text="Открыть лог файл",command=text_open)
btn1.grid()
btn2 = Button(tab1,text="Очистить файл",command=truncate_file)
btn2.grid()
btn3 = Button(tab1,text="Обновить файл",command=update_file)
btn3.grid()
text = Text(tab1,width=100,height=40)
text.grid()

#Вторая вкладка
btn4 = Button(tab2,text="Подключиться к БД",command=bd_connect).grid(sticky="w")
label_info = Label(tab2,text="Введите SQL запрос").grid()
entry_code = Entry(tab2,width=100).grid(row = 2)
btn5= Button(tab2,text="Отправить",command=bd_send).grid(sticky="w")

window.mainloop()


Сначала я подключаюсь к бд(работает), потом ввожу запрос, нажимаю кнопку отправить и в консоли появляется такая ошибка

Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Program Files\Python310\lib\tkinter\__init__.py", line 1921, in __call__
return self.func(*args)
File "e:\Desktop\programma\main.py", line 47, in bd_send
send_sql = entry_code.get()
AttributeError: 'NoneType' object has no attribute 'get'

Как правильно передать значение из виджета Entry в tkinter?
  • Вопрос задан
  • 258 просмотров
Ваш ответ на вопрос

Вопрос закрыт для ответов и комментариев

Потому что уже есть похожий вопрос.
Похожие вопросы