簡體   English   中英

無法點擊 tkinter 按鈕超過兩次

[英]Cannot click on tkinter button more than twice

我是 tkinter 的新手,並且知道有時我在做什么:D。 我創建了一個 python 腳本以從 GitHub 發布鏈接獲取 Corona Virus Stats 該腳本中有超過 20 個文件,所以我想我應該只創建一個文件來運行所有其他文件。 那么還有什么比創建 UI 更好的方法呢。 這就是我所做的,我使用 tkinter 來運行所有文件。 如果他們點擊 x 按鈕,那么我會運行 abc。 我最終通過按特定順序導入這些文件來運行這些文件(不知道這是否是最好的方法)。 所以這是我遇到錯誤的地方。 我不完全知道這個錯誤是因為我導入文件的方式還是我的 tkinter 代碼是錯誤的。 我似乎無法單擊按鈕兩次。 我會運行我的程序並單擊一個按鈕,它會正常運行,然后下次我單擊同一個按鈕時它就不起作用了。 沒有錯誤,也沒有 output。 什么都不會發生。 這是我的代碼:

#Import tkinter
import tkinter as tk
from tkinter import *
from tkinter import simpledialog
tk = tk.Tk()
Max = 190
def RunDeaths():
    #If they click on RunDeaths I will run this function
    #Check if they have already entered a path
    try:
        open('/Users/test/Documents/python/Py_Programs/Hackathon/DeathStats/Info.txt','r')
        from DeathStats import RunAll
    except:
        YourPath = simpledialog.askstring('Countries','''Please Enter Your Path To HACKATHON Folder:
    Example: 
    \"/Users/Name/Documents/python/\" 
    Note: Leave out the HACKATHON folder and you must put a slash at the end''',parent=tk)
    #Write this path to a file call Info.txt
        file = open('/Users/test/Documents/python/Py_Programs/Hackathon/DeathStats/Info.txt','w')
        file.write(str(YourPath)+'\n')
        file.write(str(Max))
        file.close()
        #Run all the files that gather the data for Corona Virus Deaths
        from DeathStats import RunAll
def RunRecoveredCases():
    #If they click on RecoveredCases Run this
    #Check If they had already entered a path
    try:
        open('/Users/test/Documents/python/Py_Programs/Hackathon/RecoveredCases/Info.txt','r')
        from RecoveredCases import RunAll
    except:
        YourPath = simpledialog.askstring('Countries','''Please Enter Your Path To HACKATHON Folder:
    Example: 
    \"/Users/Name/Documents/python/\" 
    Note: Leave out the HACKATHON folder and you must put a slash at the end''',parent=tk)
        file = open('/Users/test/Documents/python/Py_Programs/Hackathon/RecoveredCases/Info.txt','w')
        #Write there path to a file
        file.write(str(YourPath)+'\n')
        file.write(str(Max))
        file.close()
        #Run all the files that gather all the Recovered Cases
        from RecoveredCases import RunAll
#* * Here is where I think I went wrong But Im not sure
Deaths = Button(tk,height = 20, width = 30, text='Run Deaths',command = RunDeaths,highlightbackground='#000000')
Recovered = Button(tk,height = 20, width = 30, text='Run Recovered Cases',command = RunRecoveredCases,highlightbackground='#000000')
Deaths.pack()
Recovered.pack()
tk.mainloop()

所以我的問題和問題是:為什么我不能點擊一個按鈕超過兩次? 這曾經發生在我身上,我無法解決它。 任何幫助將不勝感激。(如果您想運行我的程序,因為我的解釋不夠好,這里是 git 集線器回購GitHub )謝謝

您似乎假設from RecoveredCases import RunAll將在每次導入時運行RecoveredCases.py中的代碼。 這是一個錯誤的假設。 Python 緩存導入的代碼。

The proper way to use code in a separate file is to put the code in a function or class, import the function or class exactly once, and then call the function or instantiate the class whenever you want to run the code.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM