简体   繁体   English

如何使用 Tkinter 和 Python 使用按钮命令连接小部件?

[英]How to connect a widget with a button command using Tkinter and Python?

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.这是我的问题:我正在尝试使用 Tkinter 创建一个 GUI,它可以读取文件夹中的多个文件并在界面上显示它们的值。 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。

GUI图形用户界面

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).从对此答案的早期版本的评论看来,您似乎希望并行迭代SOIListvaluesList (即:每次通过循环时,您都需要一个列表中的一个项目,另一个列表中的一个项目)。

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():
        ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM