Python Forum

Full Version: Help with tkinter Button and Label interaction issues ...
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm new to tkinter (running on python 3.5) and I'm having some issues that I'm hoping I can get some help with.  Here's my code (really simple at this point):

from tkinter import *
from tkinter.filedialog import askopenfilename
from tkinter.filedialog import askdirectory
import winsound

def findsource():
   global filename,foldername
   filename = askopenfilename(parent=root,title="Choose Source CSV File & Folder to Convert")
   if ((filename != "") and (foldername != "")):
       CSVsource.set = filename
       b1.config(state=NORMAL)
       print(filename)

def findconvert():
   global foldername,filename
   foldername = askdirectory(parent=root,title="Choose Converted CSV Location",mustexist=True)
   if ((filename != "") and (foldername != "")):
       b1.config(state=NORMAL)
       print(foldername)

def convertandsave():
   winsound.Beep(1000,500)
   print(filename)
   if (filename != ""):
       print(filename)
       try:
           with open(filename,'r') as UseFile:
               print(UseFile.read())
       except:
           print("No file exists")
   else:
       print("BLANK")


root = Tk()
root.title("NSM CSV Converter - V.4.0")
root.geometry('600x225')
root.resizable(0, 0)

filename=""
foldername=""

CSVsource=StringVar()
convertdest=StringVar()

CSVsource.set("C:\\Users\Chris Lyons\Documents\Snaps2")

errmsg = 'Error!'

l1 = Label(root, text='NSM CSV Converter', font=("Verdana",18))
l2 = Label(root, text='Source File Folder', font=("Times New Roman",9))
l3 = Label(root, text='Converted File Folder', font=("Times New Roman",9))
l4 = Label(root, textvariable=CSVsource, relief=SUNKEN, width=60, bg="white")
l5 = Label(root, textvariable=convertdest, relief=SUNKEN, width=60, bg="white")

b1 = Button(root, text='Convert and Save', font=("Verdana",12), command=convertandsave(), state=DISABLED)
b1.pack()
b1.place(x=220, y=180)
b2 = Button(root, text='Browse', font=("Verdana", 12), command=findsource)
b2.pack()
b2.place(x=450, y=75)
b3 = Button(root, text='Browse', font=("Verdana", 12), command=findconvert)
b3.pack()
b3.place(x=450, y=125)

l1.pack()
l1.pack()
l1.place(x=190, y=10)
l2.place(x=185, y=60)
l3.place(x=175, y=110)
l4.place(x=10, y=80)
l5.place(x=10, y=130)

root.mainloop()
Here's the window this code is creating:


I have 2 labels (l4 & l5) that I need to update when the user choose buttons b2 & b3 and selects a file/folder and a folder.  I'm trying to use filedialog.askopenfilename and filedialog.askdirectory to get that information from the associated selection windows.  I've tested those and they seem to be working OK.  But I'm not sure if I'm dealing with executing them in their functions correctly and getting the data back to the main program properly.  Something isn't working.

The other 'Convert and Save' button (b1) is DISABLED until the user has selected a file/folder to process and a destination folder to place the processed file into.  Once those actions are done, b1 is set to NORMAL and is active.  This appears to be working correctly but the button doesn't appear to be running the convertandsave() function when pressed.  Can't figure out why.  Any ideas?

Again, new to tkinter and I'm sure I'm doing something stupid but I need to move on, so please guide me if you can.  Appreciate any help.