首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >分裂TKInter函数

分裂TKInter函数
EN

Stack Overflow用户
提问于 2014-04-26 12:26:05
回答 2查看 218关注 0票数 0

我对所有语言的编程都很陌生,并且对某些python很感兴趣。到目前为止,我已经编写了一个函数集合,这些函数可以工作,所以当我陷入困境时,我可以参考它们。为了将它们收集在一起,我使用tkinter为我做了‘AddToCollection.py. do’。当我运行我在其中创建的.py文件时,我可以让它工作,但是我想将它导入到我想要的任何函数中。每当我使用导入的AddToCollection运行代码时,它就会立即运行。我尝试将其拆分为函数,这样新窗口只有在调用函数时才会打开,但之后它无法访问条目以获取文件名。我会感谢你的帮助。TL;DR如何在导入代码时阻止它运行?

代码语言:javascript
复制
from tkinter import * 
from tkinter import messagebox as box

#pops up a box to confirm you want to save it   
def SaveConf():
    var = box.askokcancel('Save', 'Are you sure you want to save?')    
    if var == 1:
        Save(FileName.get())     



#Does the actual saving
def Save(Oldfile):
    file = open(Oldfile+".py", 'r')
    ToAdd = '\n#- '+ Oldfile +' -------------\n' + file.read() + '\n#-----------'
    file.close()
    newfile = open("/New.py", 'a+')
    newfile.write(ToAdd)
    newfile.close()
    newwind.destroy()


#setting up items in window

#Initialising window
newwind = Tk()
newwind.title('Save a file')

#to enter filename
Frame3 = Frame()
Frame3.pack(padx=5, pady=5)
FileName = Entry(Frame3)
FileName.pack(side = LEFT)

#click button
SaveBtn2 = Button(Frame3, text = 'Save to Testicles.py', command = SaveConf)
SaveBtn2.pack(side=RIGHT,padx=2)
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-04-26 14:08:54

构建tkinter应用程序的一种常见方法是将Tk子类并在构造函数中创建小部件。下面是一个如何为代码构建体系结构的示例。它将应用程序打包到一个类(Tk的子类)中,并提供一个助手函数launch_app,用于初始化类并在其上运行主循环。

__name__ == "__main__"的要点是将脚本运行时执行的代码与导入模块时执行的代码分隔开来。如果希望在用作脚本时提供默认行为,以及从另一个模块使用该功能的能力,请将其放入一个函数中,并从if __name__ == "__main__"块调用该函数。

我还冒昧地将您的代码转换为python编码标准(用PEP 8描述)。

代码语言:javascript
复制
import tkinter as tk
from tkinter import messagebox as box

#Does the actual saving
def save(oldfile):
    file_ = open(oldfile+".py", 'r')
    #[...]
    newfile.close()
    #do not destroy window here

class App(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.title('Save a file')

        frame3 = tk.Frame()
        frame3.pack(padx=5, pady=5)
        self.filename = tk.Entry(frame3)
        self.filename.pack(side=tk.LEFT)

        #click button
        save_btn2 = tk.Button(frame3, text='Save to Testicles.py', command=self.save_conf)
        save_btn2.pack(side=tk.RIGHT, padx=2)

    def save_conf(self):
        var = box.askokcancel('Save', 'Are you sure you want to save?')
        if var == 1:
            save(self.FileName.get())
            self.destroy()  #<-- here come the destroy

def launch_app():
    app = App()
    app.mainloop()

if __name__ == "__main__":
    launch_app()
票数 1
EN

Stack Overflow用户

发布于 2014-04-26 12:42:43

如果我正确理解了这一点,那么您只想导入和使用您编写的函数吗?我觉得你错过的部分原因是:

代码语言:javascript
复制
if __name__ == "__main__":
    #setting up items in window

    #Initialising window
    newwind = Tk()
    newwind.title('Save a file')

    #to enter filename
    Frame3 = Frame()
    Frame3.pack(padx=5, pady=5)
    FileName = Entry(Frame3)
    FileName.pack(side = LEFT)

这将阻止在将文件作为模块导入时运行此代码。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23310864

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档