Handbook of Computer Programming with Python by Xanthidis Dimitrios;Manolas Christos;Xanthidou Ourania K.;Wang Han-I;

Handbook of Computer Programming with Python by Xanthidis Dimitrios;Manolas Christos;Xanthidou Ourania K.;Wang Han-I;

Author:Xanthidis, Dimitrios;Manolas, Christos;Xanthidou, Ourania K.;Wang, Han-I;
Language: eng
Format: epub
Publisher: CRC Press LLC
Published: 2022-10-17T00:00:00+00:00


7.4.3 The SELECT Statement Using GUI

Arguably, if one aims to develop a user-oriented application, it is necessary to wrap the application with a user-friendly GUI. An extensive introduction to the most important GUI widgets (e.g., labels, entry boxes, radio buttons, buttons) and their application is provided in earlier chapters of this book. In the current context, it is assumed that the focus is on the creation of a grid-based layout that will be used to host the results of the SQL queries. In such a case, a grid layout manager could be used. The following Python script showcases the development and execution of a condition-based MySQL SELECT query using a fully deployed GUI:

1 import mysql.connector 2 import tkinter as tk 3 from tkinter import ttk 4 5 global tableName, attributeName, radioButton, textVar 6 global minLabel, maxLabel, textualLabel; global textualEntry 7 global selectionsFrame, resultsFrame; global columnName, columnType 8 global minCondScale, maxCondScale; global tablesCombo, columnsCombo 9 global connect, cursor, config; global tables, columns 10 global minCond, maxCond; global minValue, maxValue, numCols 11 12 # Create the frame to select the table for the query and its attributes 13 def selectionGUI(): 14 global tables, columns; global tablesCombo, columnsCombo 15 global tableName, radioButton, textVar 16 global selectionsFrame, resultsFrame 17 global minLabel, maxLabel, textualLabel 18 global minCondScale, maxCondScale; global textualEntry 19 20 # The frame for the query selections of the user 21 selectionsFrame=tk.LabelFrame(winFrame, text='Query selections') 22 selectionsFrame.config(bg = 'light grey', fg = 'red', bd = 2, 23 relief = 'sunken') 24 selectionsFrame.grid(column = 0, row = 0) 25 26 # Create the combobox to hold the tables available in the db 27 tablesLabel = tk.Label(selectionsFrame, 28 text = "Tables available:", bg = "light grey") 29 tablesLabel.grid(column = 0, row = 0) 30 tablesCombo = ttk.Combobox(selectionsFrame, 31 textvariable = tableName, width = 15) 32 tablesCombo['values'] = tables; tablesCombo.current(0) 33 tablesCombo.grid(column = 1, row = 0) 34 35 # Button updates the attributes combo based on the table selection 36 updateAttributesButton = tk.Button(selectionsFrame, 37 text = 'Update Attributes', relief = 'raised', width = 15) 38 updateAttributesButton.bind('<Button-1>', 39 lambda event: updateAttributes()) 40 updateAttributesButton.grid(column = 2, row = 0) 41 42 # Create the button to run the query 43 runButton = tk.Button(selectionsFrame, text = 'Run Query', 44 relief = 'raised', width = 15) 45 runButton.bind('<Button-1>', lambda event: runQuery()) 46 runButton.grid(column = 3, row = 0) 47 48 # Update the columns combo based on the table selection 49 columnsLabel = tk.Label(selectionsFrame, 50 text = "Select attribute:", bg = "light grey") 51 columnsLabel.grid(column = 0, row = 1) 52 columnsCombo = ttk.Combobox(selectionsFrame, 53 textvariable = attributeName, width = 15) 54 columnsCombo.grid(column = 1, row = 1) 55 56 # Check whether selected attribute is numeric or text 57 numericalAttribute = tk.Radiobutton (selectionsFrame, 58 text = 'Numerical
attribute', width = 10, height = 2, 59 bg = 'light green', variable = radioButton, value = 1, 60 command = radioClicked).grid(column = 2, row = 1) 61 textAttribute = tk.Radiobutton (selectionsFrame, 62 text = 'Text
attribute', width = 10, height = 2, 63 bg = 'light green', variable = radioButton, value = 2, 64 command = radioClicked).



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.