0

This is my first question so be gentle!

Situation: I have two scripts that each use tkinter to prompt for some info. They are separate because sometimes I'll need one, sometime I'll need both.

I also have a main script that imports the two tkinter scripts and calls them.

When I run each tkinter by itself, or from the main script without the other script imported, it works fine. When I import both scripts, a blank tkinter (title = "tk") window pops up.

Questions: 1. What is causing this? 2. How can I prevent that stupid window from popping up? 3. Is there an automated way to see that extra window and kill it when it does show?

What I've done:

  • Googled my hiney off
  • Renamed Tk import so each script has its own instance
  • Banged my head on the desk

Code: uidpassform (first script that directly uses tkinter)

from tkinter import Tk as lf2Tk
from tkinter import Label, StringVar, Entry, Button

o_window = lf2Tk()
username = StringVar()
password = StringVar()

def close_dialog():
    o_window.destroy()

def uidpass form():
    #Return the value of the selected option
    o_window.title('Oracle Login Credentials')

    #username label and text entry box
    Label(o_window, text="User Name").grid(row=2, column=0,padx = 5,pady = 20)
    Entry(o_window, textvariable=username).grid(row=2, column=2, padx=20)  

    #password label and password entry box
    Label(o_window,text="Password").grid(row=3, column=0,padx = 5)
    Entry(o_window, textvariable=password, show='*').grid(row=3, column=2,padx = 20)  

    #login button
    Button(o_window, text="Connect", command=close_dialog).grid(row=4, column=3,columnspan=2, padx = 20)
    Label(o_window, text="").grid(row=5)  

    o_window.mainloop()
    print (username.get())
    print (password.get())
    return [username.get(), password.get()]

if __name__ == "__main__":
    uidpassform()

radiobutton form (second tkinter form)

from tkinter import Tk as rbTK
from tkinter import Radiobutton, Button, Label, IntVar, LEFT, W

rb_window = rbTK()
v = IntVar()

def validate():
    #Display the value of the selected option for debugging purposes
    value = v.get()
    return (value)

def close_dialog():
    #Check to see if the Radiobutton has a value and if so, close the form
    value = v.get()
    if value == 0:
        value = v.get()

    else:
        rb_window.destroy()

def fnradiobutton():
    #Return the value of the selected option
    rb_window.title("Connection")
    Label(rb_window, text="Choose a Data Connection:", justify=LEFT, padx=20).grid(row=0, sticky=W)
    Radiobutton(rb_window, text="Production", variable=v, value=1, command=validate).grid(row=1, padx=20, sticky=W)
    Radiobutton(rb_window, text="Test", variable=v, value=2, command=validate).grid(row=2, padx=20, sticky=W)
    Button(rb_window, text="Done", command=close_dialog).grid(row=3, column=2, padx=10)
    Label(rb_window, text="").grid(row=4)
    #print(str(V.get()) + " from fnradiobutton")

    rb_window.mainloop()
    return v.get()

if __name__ == "__main__":
    fnradiobutton()

Main script (calls the two tkinter scripts)

import cx_Oracle
import lf2 as lf
import time
import radiobutton as rb
import pyodbc


def oracle_connection():
    #Get Oracle UserID and password
    uidpass_list = lf.uidpassform()

    #Create the connection
    try:
        oracle_conn = cx_Oracle.connect(uidpass_list[0], uidpass_list[1], 'robanner', encoding='UTF-8')
    except:

    return oracle_conn

def dbconnect(switch):
    #Choose the proper connection, based on the radiobutton form
    if switch == 1:
        connection = pyodbc.connect('DSN=PCard_Production;Trusted_Connection=yes;')
    else:
        connection = pyodbc.connect('DSN=PCardTest;Trusted_Connection=yes;')
    return connection

def run_script():
    start = time.time()
    oracle_conn = oracle_connection()

    swich = rb.fnradiobutton()
    conn = dbconnect(swich)

if __name__ =='__main__':
    # This is the summary spot    
    run_script()
T-Rex
  • 1
  • 1
  • Read [Why are multiple instances of Tk discouraged?](https://stackoverflow.com/questions/48045401/why-are-multiple-instances-of-tk-discouraged) and [Tkinter understanding mainloop](https://stackoverflow.com/questions/29158220/tkinter-understanding-mainloop) – stovfl Mar 10 '20 at 16:14
  • Instead of using multiple tkinter apps as imports write them as Toplevel windows that take the root window of your 3rd script. Probably best to write them as a class – Mike - SMT Mar 10 '20 at 18:06
  • The two scripts that use tk are used broadly in other areas so I'd prefer to keep them separate. If I have to go class and toplevel, I'll make do. Was trying to avoid that. – T-Rex Mar 10 '20 at 18:31

1 Answers1

0

Try putting code for creating Root Window into functions and have them return variables. Then you have to modify your other functions to use the variables.

def initialize():
    o_window = lf2Tk()
    username = StringVar()
    password = StringVar()
    return o_window, username, password

def close_dialog(o_window):
    o_window.destroy()
...
def initialize():
    rb_window = rbTK()
    v = IntVar()
    return rb_window, v

def validate(v):
    #Display the value of the selected option for debugging purposes
    value = v.get()
    return (value)
...

Then in your main script:

def oracle_connection():
    #Get Oracle UserID and password
    o_window, username, password = lf.initialize()
    uidpass_list = lf.uidpassform(o_window, username, password)

    #Create the connection
    try:
        oracle_conn = cx_Oracle.connect(uidpass_list[0], uidpass_list[1], 'robanner', encoding='UTF-8')
    except:

    return oracle_conn
...
ywbaek
  • 2,971
  • 3
  • 9
  • 28
  • I think I'm missing something obvious. My apologies. I made the changes you suggested and at least at first blush, it appears to work. in the first tkinter code, I also had to change the button to #login button Button(o_window, text="Connect", command=close_dialog(o_window)).grid(row=4, column=3,columnspan=2, padx = 20) Label(o_window, text="").grid(row=5) o_window.mainloop() print (username.get()) print (password.get()) But when I do that, it either won't close the form says that the can't invoke button because the application has been destroyed. – T-Rex Mar 10 '20 at 17:12