首页 > 解决方案 > 我正在尝试使用 PySimpleGUI 构建一个 python GUI 数据库比较器我在这里遗漏了什么?

问题描述

我正在尝试构建一个分为两个布局窗口的 GUI DB 比较器。

  1. 从用户获取数据库详细信息并测试连接
  2. 获取一个 Excel 文件,其中包含将在 DB 上执行的 SQL 语句,并使用 Datacompy 比较将显示给用户。

我现在面临的问题是

  1. 数据库详细信息应在顶部,输出框应在它们下方,反之亦然
  2. 在输入(数据库详细信息)后,当我单击 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() 

标签: pythonpandascx-oraclepysimplegui

解决方案


您的代码看起来格式不正确。

这里列出的一些问题,

  1. 布局错误
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")],
]
  1. 获取 sg.Input 值的方法错误
uname_d1 = window.FindElement('-uname_db1-')    # It can be window['-uname_db1-']

它只是获取 element 的元素sg.input。获得价值sg.Input

uname_d1 = values['-uname_db1-']
  1. 如果后面的逻辑语句中Test_DB1和的值都会得到错误的结果Test_DB2
if (Test_DB1 and Test_DB2 != 'True'):

也许应该是

if not (Test_DB1 and Test_DB2):
  1. 调用时函数的参数顺序错误create_con
#      def createCon(uname,   passw,   hname,   portnum, sname):
Test_DB1 = createCon(uname_d1,pword_d1,hname_d1,sname_d1,port_d1)
  1. portnum可以在传递给函数之前转换为整数createCon

推荐阅读