Search code examples
pythontkintertextbox

How to add a custom tkinter textbox in my Python app?


So I have been using python to do my 'ETLs' from SQL to Excel or CSV. I have just found it to be faster and easier than using SSIS.

While I do this in Jupyter I thought it would be a fun exercise to package this into an app that I can share with others. I'm not the most versed in tkinter and am trying to add a custom sized textbox that would be a little nicer than standard textbox for copy/pasting query. The issue is when I add the textbox, it instead adjusts the settings for the app window.

Here is what I have so far:

#Load Libraries
import pandas as pd
import numpy as np
import pyodbc
import xlsxwriter
from pandas import ExcelWriter 
import openpyxl
import os
import datetime
from tkinter import *

#create window    
def conVar():
    server = e1.get()
    db =  e2.get()
    un = e3.get()
    pw = e4.get()

    conn=("DRIVER={SQL Server};SERVER=%s;DATABASE=%s;UID=%s;PWD=%s" % (server, db, un, pw))
    print(conn)

root = Tk()

root.title("SQL2Excel")
root.geometry("500x500")

Label(root, text="Server").grid(row=0)
Label(root, text="Database").grid(row=1)
Label(root, text="Username").grid(row=2)
Label(root, text="pw").grid(row=3)
Label(root, text="Tables").grid(row=6)
Label(root, text="or Enter Query").grid(row=7)

variable = StringVar(root)
variable.set("                                                                       ") 

w = OptionMenu(root, variable, "test1", "test2", "test3")
r = root
r.geometry("250x150")

t = Text(r, height=20, width=40)

e1 = Entry(root)
e2 = Entry(root)
e3 = Entry(root)
e4 = Entry(root, show="*")

e1.grid(row=0, column=1)
e2.grid(row=1, column=1)
e3.grid(row=2, column=1)
e4.grid(row=3, column=1)
w.grid(row=6, column=1)
t.grid(row=8, column=1)


Button(root, text='Quit', command=root.destroy).grid(row=4, column=0, sticky=W, pady=4)
Button(root, text='Test Connection', command=conVar).grid(row=4, column=1, sticky=W, pady=4)

root.mainloop()

The idea is that once connection info is entered I will pull a list of tables into the combobox or let the user copy and paste a query into the larger text box. I really feel like I'm missing something easy but struggling at this point.

Can someone help show me the error of my ways? Thank you.


Solution

  • Your code is change the size of the root window

    root.title("SQL2Excel")
    root.geometry("500x500") 
    r = root
    r.geometry("250x150")
    

    Instead, try changing the geometry of the Combobox.

    Also, I do not see a Combobox in your code