在python的tkinter库中,弹出新框架时如何使其他控件呈现灰色且处于禁用状态
例如在酷狗音乐中,当点击新建歌单时将弹出窗口,此时背景所有框架呈灰色且处于禁用状态,如何在python的tkinter实现此功能?
在python的tkinter库中,弹出新框架时如何使其他控件呈现灰色且处于禁用状态
例如在酷狗音乐中,当点击新建歌单时将弹出窗口,此时背景所有框架呈灰色且处于禁用状态,如何在python的tkinter实现此功能?
【相关推荐】
前面主要是调用python的pymysql进行操作,实现简单的写入和查询,这里面存在很多问题,比如重复ID写入等,文章主要是做一些简单的功能,并不打算实现数据库的事务等,以及类似于商城等系统设计。这部分主要是使用tkinter实现一个查询界面,调用pymysql进行数据查询.tkinter参照的书主要是Manning出版社的Python and Tkinter Programming,封面长这个样子的(希望Manning能给我打钱~)。在进行介绍前友情提醒Mac系统是10.14.6的朋友,千万不要用这个!!!,系统bug会引起反复重启,别问我怎么知道的,具体可以参照
issue37833和tkinter mainloop() function logs me out of my mac首先,使用python的tkinter制作框架,即定义一个框架
def frame(self,side):
w = Frame(self)
w.pack(side=side,expand = YES, fill = BOTH)
return w
在这之后,需要定义按钮,比如你需要点击
def button(self,side,text,fg = None,command = None):
w = Button(self,text= text,fg = fg,command = command)
w.pack(side = side,expand = YES,fill = BOTH)
然后按照上述书籍定义不同的功能组件即可,最后是写入对应的查询操作
from tkinter import *
import tkinter.messagebox as messagebox
from get_query import excute_query,export_results
import os
import pandas as pd
class drawGraph(Frame):
def __init__(self):
Frame.__init__(self)
self.pack(expand=YES,fill=BOTH)
self.master.geometry("500x600")
self.master.title("SQL查询器")
self.master.iconname("查询界面")
def frame(self,side):
w = Frame(self)
w.pack(side=side,expand = YES, fill = BOTH)
return w
def button(self,side,text,fg = None,command = None):
w = Button(self,text= text,fg = fg,command = command)
w.pack(side = side,expand = YES,fill = BOTH)
return w
def text_frame(label):
input1 = Label(root,text =label).pack()
input_text = StringVar()
name = Entry(root,width = 40,textvariable = input_text,pdx = 20,pdy = 10).pack()
input_text.set(" ")
return input_text,name
display = StringVar()
newEntry = Entry(self,relief = RAISED,textvariable = display,width = 1)
newEntry.pack(side = TOP,expand = YES, fill = BOTH)
def get_query():
query_context = newEntry.get()
rows,cols = excute_query(query_context)
text = Text(self,height = 26,width = 150)
text.insert(END,export_results(rows,cols))
text.pack()
# if query_context:
# Message(self,text = export_results(rows,cols),relief= SUNKEN).pack()
# else:
# Message(self,text = "请输入查询语句或检查SQL语法!" ).pack()
return export_results(rows,cols)
opsF =frame(self,TOP)
for char in ["查询","清空"]:
if char == "查询":
btn = button(opsF,LEFT,char,command= get_query)
if char =="清空":
btn = button(opsF,LEFT,char,fg = "blue",command=lambda w=display: w.set(""))
#rows
# Label(self,text = "输入下载地址:").pack(side = LEFT,padx=5,pady = 10)
# e=StringVar()
# SecondEntry = Entry(self,relief = RAISED,textvariable = e,width = 1)
# SecondEntry.pack(side = TOP,expand = YES, fill = BOTH)
opf = frame(self,TOP)
displayNew = StringVar()
temp_entry = Entry(opf,relief = RAISED,textvariable = displayNew, width = 1)
temp_entry.pack(side =LEFT,expand = YES, fill = BOTH)
displayName = StringVar()
temp_Name = Entry(opf,relief = RAISED,textvariable =displayName, width = 1)
temp_Name.pack(side = LEFT,expand = YES, fill = BOTH)
def export_result_query():
pathName = temp_entry.get()
name = temp_Name.get()
query_context = newEntry.get()
rows,cols = excute_query(query_context)
df_data =export_results(rows,cols)
df_data.to_excel("{0}{1}.xlsx".format(pathName,name))
# Message(self,text = '{0}{1}'.format(pathName,name),relief= SUNKEN).pack()
button(self,BOTTOM,"退出查询器",fg= 'red',command=exit)
opsB =frame(self,BOTTOM)
for char in ["输出结果","清空路径","清空名称"]:
if char == "输出结果":
btn = button(opsB,LEFT,char,command = export_result_query)
if char == "清空路径":
btn = button(opsB,LEFT,char,fg= 'red',command=lambda w=displayNew: w.set(""))
if char == "清空名称":
btn = button(opsB,LEFT,char,fg= 'red',command=lambda w=displayNew: w.set(""))
在上面定义完了查询界面组件以及对应的查询按钮功能,下面就需要定义对应的数据库连接和对应的操作了(export函数放在exucte里面最好不过了)
import pymysql
import pandas as pd
from CON_CONSTS import ip_port,host,user,passwd,db
def excute_query(context):
try:
con = pymysql.connect(host = host,port = ip_port,\
user = user,\
passwd = passwd,\
db = db, \
charset = 'utf8')
cur = con.cursor()
cur.execute(context)
except:
print ("连接错误!")
finally:
if con:
con.close()
cols = []
for col in cur.description:
cols.append(col[0])
rows = cur.fetchall()
#print(rows)
return rows,cols
def export_results(rows,desc):
new_row = []
new_row.append(desc)
for row in rows:
new_row.append(list(row))
return pd.DataFrame(new_row[1:],columns=new_row[0])
在CONST.py文件里面保存对应的连接数据即可
'''
ip_port: 端口
host: 主机
user: 用户名
passwd: 密码
db: 连接数据库
'''
最后我们在start.py中定义启动
from excute_func import drawGraph
if __name__ == "__main__":
drawGraph().mainloop()
至此,完成了最基本的配置,因为我的系统升级到了10.14.6,因此也就不能一边操作一边截图,保存最基本的验证了, 因此只能把系统版本之前的代码贴出来.