-1

This is my problem: I'm trying to create a GUI with Tkinter that can read multiple files in folders and show their values on the interface. But when I press the widget button nothing happens.

This is the script that I use, feel free to ask your questions if you don't understand my process.

I will be very thankful if someone can help me.

    for s in SOIList and valuesList:

        if s == selectedConfig.get() and s == selectedConfig.get():

           textfilespathB = 'C:/temp/dossiersoi2/'+s+'SOI.txt'
           textfilespathC = 'C:/temp/fichiervalues2/'+s+'val.txt'

           with open(textfilespathB, "r") as f:
               frame2 = Frame(root, width = 50, height = 100)
               frame2.grid(row = 2, column = 0)
               Label(frame2, text=f.read()).pack()

           with open(textfilespathC, "r") as f:
               frame4 = Frame(root, width = 50, height = 100)
               frame4.grid(row = 2, column = 1)
               Label(frame4, text=f.read()).pack()

        else:
            print("not working")

This is the GUI related to my script.

GUI

Nikola.L
  • 9
  • 3
  • 2
    Please post a **minimal**, **complete**, **verifiable** example - cf https://stackoverflow.com/help/mcve. Your current snippet is neither minimal nor complete. – bruno desthuilliers Sep 28 '18 at 10:21
  • 1
    What have you done to debug this? Have you added some print statements to verify whether the command was called or not? Did you verify the values of variables immediately before conditional checks, and inside each iteration of a loop? – Bryan Oakley Sep 28 '18 at 11:23
  • @brunodesthuilliers sorry it's my first post on this website, I will change my post asap :) – Nikola.L Sep 28 '18 at 11:41
  • @BryanOakley I put a print in my "if loop" to see what is going on and I obtained only the output of my print. This means that the problem is in my fonction (def findGoodFile) because everything works well, there is just this probrem when I assign the command (findGoodFile) to my button – Nikola.L Sep 28 '18 at 11:49

1 Answers1

0

Consider this line of code:

for s in SOIList and valuesList:

It is not doing what you expect. From the comments to an earlier version of this answer it appears you want to iterate over SOIList and valuesList in parallel (ie: each time through the loop you want one item from one list and one item for the other).

The way to do that is explained nicely in the answers to this question: How to iterate through two lists in parallel?

In your case the code would look like this:

for s, v in zip(SOIList, valuesList):
    if s == selectedConfig.get() and v == selectedConfig.get():
        ...
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • @Nikola.L: there's no problem with that line, other than the fact that you're making the same check twice. – Bryan Oakley Sep 28 '18 at 11:53
  • Thank you for your reply. I tried what you said but it didn't work. It's like there's a problem with the line : if s == selectedConfig.get() and s == selectedConfig.get(): @BryanOakley The idea of this condition is when the name of the element in SOIList and valuesList is the same that the name of the element that we select in the option menu, we show the values of the concerning files. – Nikola.L Sep 28 '18 at 11:58
  • @Nikola.L: each time through the loop, are you expecting to take one item from `SOIList` and one item from `valuesList`, or are you expecting to take just one item at a time from one list until it runs out, and then take one item from the other list? – Bryan Oakley Sep 28 '18 at 12:01
  • Yes this is what I want to do, take the first item from SOIList and valuesList and compare them to the item of my option menu, and if they are the same then I want to print on my GUI the values of the concerning files. Now when I run my script I obtain 6 times "not working" by my print command. – Nikola.L Sep 28 '18 at 12:05
  • Thank you again. I put a print after the 'for' and I obtained the good items. but for the 'if' loop it didn't printed the values of the files on the GUI, I obtained just the print command of my 'else'. I think there is maybe a problem with the 'if'. It seems that s and v can't match with the item that we obtain with get(). – Nikola.L Sep 28 '18 at 12:25
  • @Nikola.L: you need to make sure the types are the same -- compare strings to strings or integers to integers. – Bryan Oakley Sep 28 '18 at 12:29
  • It works ! there was a problem with my files name instead of having 'cubeconfig' I had 'configcube', this is why I obtained the print of my 'else' command. I will reply to my question so people can use my work if they have the same problem. Thank you again :) – Nikola.L Sep 28 '18 at 12:40