python - 我正在尝试使用 PySimpleGUI 构建一个 python GUI 数据库比较器我在这里遗漏了什么?
问题描述
我正在尝试构建一个分为两个布局窗口的 GUI DB 比较器。
- 从用户获取数据库详细信息并测试连接
- 获取一个 Excel 文件,其中包含将在 DB 上执行的 SQL 语句,并使用 Datacompy 比较将显示给用户。
我现在面临的问题是
- 数据库详细信息应在顶部,输出框应在它们下方,反之亦然
- 在输入(数据库详细信息)后,当我单击 DB Test1 按钮应用程序关闭本身而不显示出了什么问题(没有代码错误,刚刚消失)
我是 PySimpleGUI 的新手,可能犯了一些愚蠢的错误,请指导我完成我需要重新输入的错误和语句。
这是第一个布局窗口的代码:
import PySimpleGUI as sg
import re
import datacompy
import cx_Oracle
import pandas as pd
def read_query(connection, query):
cursor = connection.cursor()
try:
cursor.execute( query )
header = [ x[0] for x in cursor.description]
rows = cursor.fetchall()
return header, rows
finally:
if cursor is not None:
cursor.close()
def createCon(uname,passw,hname,portnum,sname):
dsn_str = cx_Oracle.makedsn(host=hname,
port=portnum,
service_name = sname)
con = cx_Oracle.connect(user = uname,
password = passw,
dsn = dsn_str)
if con == True:
return con
else:
return 0
DB_creds_one = [
[
sg.Text("UserName"),
sg.In(size=(30, 2), enable_events=True, key="-uname_db1-")
],
[
sg.Text("Password"),
sg.In(size=(30, 1), enable_events=True, key="-pword_db1-")
],
[
sg.Text("Hostname"),
sg.In(size=(30, 1), enable_events=True, key="-hname_db1-")
],
[
sg.Text("Service Name"),
sg.In(size=(30, 1), enable_events=True, key="-sname_db1-")
],
[
sg.Text("Port"),
sg.In(size=(30, 1), enable_events=True, key="-port_db1-")
],
[
sg.Button('Test Con1', key='B1')
],
]
DB_creds_two = [
[
sg.Text("UserName"),
sg.In(size=(25, 1), enable_events=True, key="-uname_db2-")
],
[
sg.Text("Password"),
sg.In(size=(25, 1), enable_events=True, key="-pword_db2-")
],
[
sg.Text("Hostname"),
sg.In(size=(25, 1), enable_events=True, key="-hname_db2-")
],
[
sg.Text("Service Name"),
sg.In(size=(25, 1), enable_events=True, key="-sname_db2-")
],
[
sg.Text("Port"),
sg.In(size=(25, 1), enable_events=True, key="-port_db2-")
],
[
sg.Button('Test Con2',key='B2')
],
]
layoutprefile = [
[
sg.Column(DB_creds_one),
sg.VSeperator(),
sg.Column(DB_creds_two),
[sg.Output(size=(61, 5), key='-output-')],
[sg.Submit('Proceed'), sg.Cancel('Exit')]
]
]
window = sg.Window("DB Comparator", layoutprefile)
while True: # The Event Loop
event, values = window.read()
# print(event, values) # debug
if event in (None, 'Exit', 'Cancel'):
secondwindow = 0
break
elif event == 'B1':
# Test DB Connection 1 return Test_DB1
uname_d1 = window.FindElement('-uname_db1-')
pword_d1 = window.FindElement('-pword_db1-')
hname_d1 = window.FindElement('-hname_db1-')
sname_d1 = window.FindElement('-sname_db1-')
port_d1 = window.FindElement('-port_db1-')
if uname_d1 and pword_d1 and hname_d1 and sname_d1 and port_d1 == "":
print("ENter values")
else:
Test_DB1 = createCon(uname_d1,pword_d1,hname_d1,sname_d1,port_d1)
elif event == 'B2':
# Test DB Connection 2 return Test_DB2
uname_d2 = window.FindElement('-uname_db2-')
pword_d2 = window.FindElement('-pword_db2-')
hname_d2 = window.FindElement('-hname_db2-')
sname_d2 = window.FindElement('-sname_db2-')
port_d2 = window.FindElement('-port_db2-')
if uname_d2 and pword_d2 and hname_d2 and sname_d2 and port_d2 == "":
print("ENter values")
else:
Test_DB2 = createCon(uname_d2,pword_d2,hname_d2,sname_d2,port_d2)
if event == 'Proceed':
if (Test_DB1 and Test_DB2 != 'True'):
secondwindow = 0
sg.Popup("Incorrect Database Details Please Verify the Connection Again")
else:
window.close()
secondwindow = 1
break
if secondwindow != 1:
exit()
解决方案
您的代码看起来格式不正确。
这里列出的一些问题,
- 布局错误
layoutprefile = [
[
sg.Column(DB_creds_one),
sg.VSeperator(),
sg.Column(DB_creds_two),
[sg.Output(size=(61, 5), key='-output-')],
[sg.Submit('Proceed'), sg.Cancel('Exit')]
]
]
如果需要横向布局
layout = [
[sg.Button("Hello1"), sg.VerticalSeparator(), sg.Button("Hello2")],
]
或垂直布局
layout = [
[sg.Button("Hello1")],
[sg.HorizontalSeparator()],
[sg.Button("Hello2")],
]
- 获取 sg.Input 值的方法错误
uname_d1 = window.FindElement('-uname_db1-') # It can be window['-uname_db1-']
它只是获取 element 的元素sg.input
。获得价值sg.Input
uname_d1 = values['-uname_db1-']
- 如果后面的逻辑语句中
Test_DB1
和的值都会得到错误的结果Test_DB2
if (Test_DB1 and Test_DB2 != 'True'):
也许应该是
if not (Test_DB1 and Test_DB2):
- 调用时函数的参数顺序错误
create_con
。
# def createCon(uname, passw, hname, portnum, sname):
Test_DB1 = createCon(uname_d1,pword_d1,hname_d1,sname_d1,port_d1)
portnum
可以在传递给函数之前转换为整数createCon
推荐阅读
- c - 从Linux上的存储卡读取原始数据
- .htaccess - htaccess 在带有 css/js 文件夹和多 web 的子文件夹中设置主页
- mysql - 如何在一个 SQL 查询中选择同一个表中的相关项目
- python - 用于橡皮筋的鼠标左键和用于平移 PYQT 的中键
- python - 如何将一个 ponyorm 实体属性与其他几个实体相关联?
- windows - CEdit不会自动设置焦点
- python - Excel 集成 - 如何使用 python Openpyxl 在特定范围的单元格中附加数据框
- python - 使用 PyQt5 轻松实现多线程,用于更新 QTextBrowser 内容
- artificial-intelligence - 学习的基于规则的系统是否被视为强化学习?
- python - 使用 Mypy 键入 dict mixin 类