0

I was able to connect mysql and python

enter image description here

I've create 2 labels, and I want to import the value from the Database(MySQL) to the label(tkinter) instead of 'text1' and 'text2'.

enter image description here

What is the function that i have to use? or How can i solve that?

from tkinter import *
from PIL import Image, ImageTk
import pymysql

conn = pymysql.connect(host = "localhost",
                       user = "root",
                       passwd = "asdf",
                       db = "testdb", charset = 'utf8')


curs = conn.cursor(pymysql.cursors.DictCursor)
sql = "select * from test where id=%s and text1=%s and text2=%s"
curs.execute(sql,('A0002','asdfasdfasdf', 'asdfasdfvvwervfvasff'))

class App(Frame):
    def __init__(self, master):
        Frame.__init__(self, master)
        self.grid(row=0)
        self.columnconfigure(0,weight=1)
        self.rowconfigure(0,weight=1)
        self.original = Image.open('Chrysanthemum.jpg')
        resized = self.original.resize((400, 300),Image.ANTIALIAS)
        self.image = ImageTk.PhotoImage(resized)
        self.display = Label(self, image = self.image)
        self.display.grid(column=0,row=0)

root = Tk()
root.title("image test")
root.geometry("1000x800+100+100")
root.resizable(0,0)
root.configure(background = 'white')




app = App(root)
rows = curs.fetchall()

label1 = Label(root,text = "text1")
label1.config(wraplength =500)
label1.config(width=80,height=20)
label1.grid(column=1,row=0)

label2 = Label(root,text ="text2")
label2.config(wraplength=910)
label2.config(width=138,height=30)
label2.grid(columnspan=2,row=1)

app.mainloop()
Lee
  • 3
  • 4
  • Can you be more clearer on what the output from the database looks like and which values you want to assign to `text1` and `text2` – v.coder Nov 27 '17 at 04:18

2 Answers2

0

To set the text of a label in Tkinter you can use StringVar and set it's value accordingly. Please see if the below code helps:

labelText1 = Stringvar()
label1 = Label(self, textvariable=labelText1)

The variable can be assigned using set function whenever there is update in the database:

labelText1.set(labelTextValue)
v.coder
  • 1,822
  • 2
  • 15
  • 24
0

It would be great if you can give some more explanation on your problem.

Once you load the value from the sql db, you can store it in a variable and then do an update on the text.To update the value of a label simply use

label1.config(text='New Value')

I'll try to help you out if this doesn't make any sense

More reading

Changing the text on a label

http://effbot.org/tkinterbook/label.htm

Joel Benjamin
  • 209
  • 4
  • 15