Python代码可以关闭TKINTER弹出窗口,而无需杀死实际程序

发布于 2025-01-21 20:14:05 字数 1618 浏览 0 评论 0原文

我是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

入画浅相思 2025-01-28 20:14:05

如果您想要的只是为了“走开”而无需实际关闭的tk窗口,则可以使用iconify()方法而不是destion> destion> destion> destion();这将最小化app()窗口到任务栏而无需实际关闭。

我将self.iconify()添加到collectanswers()方法的末尾。

我还凭借对您的代码风格进行了一些更改以更好地与标准的Python样式实践进行了一些更改 - 如果您还没有使用像Flake8或Black这样的Formatter之类的Linter,我建议这样做!最好习惯于在养成不良习惯之前强制执行风格指南!

import openpyxl
import tkinter as tk


class App(tk.Tk):  # root application instance
    def __init__(self,**kw) -> None:
        super().__init__()
        self.geometry("300x100")
        self.title = ("Enter Sheet Paths")
        self.resizable(width=False, height=False)
        self.grid()
        input_label = tk.Label(
            self,
            text="Give Input Sheet Path"
        )
        input_label.grid(row=0, column=0)
        self.Input_From_User1 = tk.Entry(self)
        self.Input_From_User1.grid(row=0, column=1)
    
        output_label = tk.Label(
            self,
            text="Give Output Sheet Path"
        )
        output_label.grid(row=1, column=0)
        self.Input_From_User2 = tk.Entry(self)
        self.Input_From_User2.grid(row=1, column=1)
        submit = tk.Button(
            self,
            text="Feed into Program",
            command=self.collect_answers
        )
        submit.grid(row=2, column=1)
        self.answers = {}
        
    def collect_answers(self) -> None:
        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'])
        input_path = self.answers['Input_Path']
        output_path = self.answers['Output_Path']
        self.iconify()  # MINIMIZE THE WINDOW


def main():
    app = App()
    app.mainloop()
    wb = openpyxl.load_workbook(Input_Path)
    return wb["Sheet1"]

    
if __name__ == '__main__':
    main()

If all you want is for the TK window to "go away" without actually closing, you could use the iconify() method instead of destroy(); this will minimize the App() window to the taskbar without actually closing it.

I added self.iconify() to the end of your collectAnswers() 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!

import openpyxl
import tkinter as tk


class App(tk.Tk):  # root application instance
    def __init__(self,**kw) -> None:
        super().__init__()
        self.geometry("300x100")
        self.title = ("Enter Sheet Paths")
        self.resizable(width=False, height=False)
        self.grid()
        input_label = tk.Label(
            self,
            text="Give Input Sheet Path"
        )
        input_label.grid(row=0, column=0)
        self.Input_From_User1 = tk.Entry(self)
        self.Input_From_User1.grid(row=0, column=1)
    
        output_label = tk.Label(
            self,
            text="Give Output Sheet Path"
        )
        output_label.grid(row=1, column=0)
        self.Input_From_User2 = tk.Entry(self)
        self.Input_From_User2.grid(row=1, column=1)
        submit = tk.Button(
            self,
            text="Feed into Program",
            command=self.collect_answers
        )
        submit.grid(row=2, column=1)
        self.answers = {}
        
    def collect_answers(self) -> None:
        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'])
        input_path = self.answers['Input_Path']
        output_path = self.answers['Output_Path']
        self.iconify()  # MINIMIZE THE WINDOW


def main():
    app = App()
    app.mainloop()
    wb = openpyxl.load_workbook(Input_Path)
    return wb["Sheet1"]

    
if __name__ == '__main__':
    main()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文