Python Forum
Help with tkinter Button and Label interaction issues ...
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with tkinter Button and Label interaction issues ...
#1
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Tkinter: An image and label are not appearing. emont 7 625 Mar-21-2024, 03:00 PM
Last Post: deanhystad
  User Interaction with Graph in GUI Marty23 1 372 Mar-19-2024, 03:17 AM
Last Post: deanhystad
  Interaction between Matplotlib window, Python prompt and TKinter window NorbertMoussy 3 514 Mar-17-2024, 09:37 AM
Last Post: deanhystad
  [Tkinter] TKinter Remove Button Frame Nu2Python 8 999 Jan-16-2024, 06:44 PM
Last Post: rob101
  tkinter - touchscreen, push the button like click the mouse John64 5 857 Jan-06-2024, 03:45 PM
Last Post: deanhystad
  tkinter destroy label inside labelFrame Nick_tkinter 3 4,557 Sep-17-2023, 03:38 PM
Last Post: munirashraf9821
  Centering and adding a push button to a grid window, TKinter Edward_ 15 4,814 May-25-2023, 07:37 PM
Last Post: deanhystad
  [Tkinter] how to make label or button not visible with the place method? nowayj63 2 2,811 Jan-03-2023, 06:29 PM
Last Post: Yoriz
  [Tkinter] Updating Tkinter label using multiprocessing Agusms 6 3,151 Aug-15-2022, 07:10 PM
Last Post: menator01
  [Tkinter] The Text in the Label widget Tkinter cuts off the Long text in the view malmustafa 4 4,902 Jun-26-2022, 06:26 PM
Last Post: menator01

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020