python - Tkinter Mysql 窗口

标签 python mysql user-interface tkinter

我能够连接 mysql 和 python

enter image description here

我创建了 2 个标签,我想将值从数据库 (MySQL) 导入到标签 (tkinter),而不是“text1”和“text2”。

enter image description here

我必须使用什么功能? 或者 我该如何解决这个问题?

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()

最佳答案

要在 Tkinter 中设置标签的文本,您可以使用 StringVar 并相应地设置其值。请查看以下代码是否有帮助:

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

只要数据库中有更新,就可以使用set函数对变量进行赋值:

labelText1.set(labelTextValue)

关于python - Tkinter Mysql 窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47503739/

相关文章:

python - 在值更改时使用 python 分割排序文件

python - pandas:groupby 多列,连接一列同时添加另一列

Python 3.x数组,使用排名逻辑的新数组

MySQL:使用触发器和 IF 语句更新第二个表

mysql - 通过中间 M 进行连接的嵌套查询 :N (MySQL)

javascript - React DIV动态布局算法创建盒中盒 View

java - 如何从 Java 中的图形中读取坐标?

java - 对齐 Javafx 按钮的问题

python - Matplotlib (Python) 中的偏移值

mysql - Magento 前端 404 错误