Forum >> Programmazione Python >> GUI >> Pulsante che invia dalla text_box al db di postgresql i dati

Pagina: 1

#importo librerie DB

import psycopg2

import psycopg2.extras




#importo librerie Interfaccia

from tkinter import *

from tkinter import messagebox

import tkinter as tk

from tkinter.font import BOLD, ITALIC




#INTERFACCIA

window = tk.Tk()

window.title("Server")

window.geometry('900x500')

window.resizable(True, False)

#SFONDO

window.config(bg='#1da39f')




#




#

def extract_data():

print(text_box1.get('1.0', 'end'))

print(text_box2.get('1.0', 'end'))




message =''' '''

text_box1 = Text(window,height=1.5,width=25,wrap='word')

text_box1.pack(expand=True)

text_box1.insert('end', message)







text_box2 = Text(window,height=1.5,width=25,wrap='word')

text_box2.pack(expand=True)

text_box2.insert('end', message)







Button( window,text='Login',command=extract_data).pack(expand=True)




#connessione al db

connection = psycopg2.connect(

host = "localhost",

database = "Tab1",

user = "postgres",

password = "1111",

)




#verifica connessione

print("connection successful")




with connection.cursor(cursor_factory=psycopg2.extras.DictCursor) as cur:



#Inseri nella tabella se già esiste

cur.execute('DROP TABLE IF EXISTS two')




cur = connection.cursor()




#crea tab usando cursor

cur.execute("CREATE TABLE two(name TEXT, age INT, height REAL)")




#inserire i dati nella tabella

#!cur.execute("INSERT INTO two(name, age, height) VALUES(%s,%s,%s)", ("Rex", 20, 178))




#leggere dal DB usando cursor.execute()

cur.execute("SELECT * FROM two")




rows = cur.fetchall()




for r in rows:

print(rows)




cur.close()

connection.commit()

connection.close()

window.mainloop()
Chiuso per cross post.


Pagina: 1



La discussione è chiusa.