Python代码可以关闭TKINTER弹出窗口,而无需杀死实际程序
我是Python的新手,在这里的帮助下,我起草了一个从用户输入并将其传递给其他程序的代码。
是否有一种方法可以关闭弹出窗口,而无需实际终止或关闭程序。我尝试使用destroy(),但它要么只是清除弹出消息的内容,要么弄乱了整个代码。
有人可以在这里帮我吗?以下是我起草的代码。
import openpyxl
import tkinter as tk
class App(tk.Frame):
def __init__(self,master=None,**kw):
self.answers = {}
tk.Frame.__init__(self,master=master,**kw)
tk.Label(self,text="Give Input Sheet Path").grid(row=0,column=0)
self.Input_From_User1 = tk.Entry(self)
self.Input_From_User1.grid(row=0,column=1)
tk.Label(self,text="Give Output Sheet Path").grid(row=1,column=0)
self.Input_From_User2 = tk.Entry(self)
self.Input_From_User2.grid(row=1,column=1)
tk.Button(self,text="Feed into Program",command = self.collectAnswers).grid(row=2,column=1)
def collectAnswers(self):
self.answers['Input_Path'] = self.Input_From_User1.get()
self.answers['Output_Path'] = self.Input_From_User2.get()
print("Given Input Path ", self.answers['Input_Path'])
print("Given Output Path ", self.answers['Output_Path'])
global Input_Path
global Output_Path
Input_Path = self.answers['Input_Path']
Output_Path = self.answers['Output_Path']
def main():
root = tk.Tk()
root.geometry("300x100")
App(root).grid()
root.mainloop()
wb = openpyxl.load_workbook(Input_Path)
return wb["Sheet1"]
if __name__ == '__main__':
main()
I'm new to Python and with help from here I have drafted a code that gets input from a user and pass it on to other programs.
is there a way to close the pop up window without actually terminating or closing the program. I tried using Destroy() but it either just clears the content of the pop up message or messes the whole code.
Could somebody help me out here please. Below is the code I drafted.
import openpyxl
import tkinter as tk
class App(tk.Frame):
def __init__(self,master=None,**kw):
self.answers = {}
tk.Frame.__init__(self,master=master,**kw)
tk.Label(self,text="Give Input Sheet Path").grid(row=0,column=0)
self.Input_From_User1 = tk.Entry(self)
self.Input_From_User1.grid(row=0,column=1)
tk.Label(self,text="Give Output Sheet Path").grid(row=1,column=0)
self.Input_From_User2 = tk.Entry(self)
self.Input_From_User2.grid(row=1,column=1)
tk.Button(self,text="Feed into Program",command = self.collectAnswers).grid(row=2,column=1)
def collectAnswers(self):
self.answers['Input_Path'] = self.Input_From_User1.get()
self.answers['Output_Path'] = self.Input_From_User2.get()
print("Given Input Path ", self.answers['Input_Path'])
print("Given Output Path ", self.answers['Output_Path'])
global Input_Path
global Output_Path
Input_Path = self.answers['Input_Path']
Output_Path = self.answers['Output_Path']
def main():
root = tk.Tk()
root.geometry("300x100")
App(root).grid()
root.mainloop()
wb = openpyxl.load_workbook(Input_Path)
return wb["Sheet1"]
if __name__ == '__main__':
main()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想要的只是为了“走开”而无需实际关闭的tk窗口,则可以使用
iconify()
方法而不是destion> destion> destion> destion()
;这将最小化app()
窗口到任务栏而无需实际关闭。我将
self.iconify()
添加到collectanswers()
方法的末尾。我还凭借对您的代码风格进行了一些更改以更好地与标准的Python样式实践进行了一些更改 - 如果您还没有使用像Flake8或Black这样的Formatter之类的Linter,我建议这样做!最好习惯于在养成不良习惯之前强制执行风格指南!
If all you want is for the TK window to "go away" without actually closing, you could use the
iconify()
method instead ofdestroy()
; this will minimize theApp()
window to the taskbar without actually closing it.I added
self.iconify()
to the end of yourcollectAnswers()
method.I also took the liberty of making some changes to your code's style to better align with standard Python style practices - if you aren't already using a linter like Flake8 or a formatter like Black, I would recommend doing so! Better to get used to having a style guide enforced before you develop bad habits!