Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
文件资源管理器在Tkinter Python程序中自动打开_Python_User Interface_Tkinter - Fatal编程技术网

文件资源管理器在Tkinter Python程序中自动打开

文件资源管理器在Tkinter Python程序中自动打开,python,user-interface,tkinter,Python,User Interface,Tkinter,当我运行python程序时,文件资源管理器会自动打开。我希望这只发生在用户按下“打开”按钮时 以上问题已经解决 在加密或解密文本方面,我目前遇到的问题是上传文件功能正常工作,或者键入字符串功能正常工作。两者不在同一程序中工作 我将感谢任何帮助 from tkinter import * from tkinter import filedialog class Caesar(Frame): LETTERS = "abcdefghijklmnopqrstuvwxyz" def U

当我运行python程序时,文件资源管理器会自动打开。我希望这只发生在用户按下“打开”按钮时

以上问题已经解决

在加密或解密文本方面,我目前遇到的问题是上传文件功能正常工作,或者键入字符串功能正常工作。两者不在同一程序中工作

我将感谢任何帮助

from tkinter import *
from tkinter import filedialog

class Caesar(Frame):
    LETTERS = "abcdefghijklmnopqrstuvwxyz"

    def UploadAction(self):
        filename = filedialog.askopenfilename(initialdir="/",
                                          title="Open File",
                                          filetypes=(("Text Files", "*.txt"), ("All Files", "*.*")))

        with open('caesartest.txt') as f:
        contents = f.read().splitlines()
        contents = ' '.join(map(str, contents))
        return contents

    def __init__(self, pencere):
        Frame.__init__(self, pencere)
        self.pencere = pencere

        Label(pencere, text="Enter your message: ", relief=GROOVE, width=20).place(x=20, y=30)
        self.Ent1 = Entry(pencere, width=30)
        self.Ent1.place(x=230, y=30)

        Label(pencere, text="Upload a .txt file: ", relief=GROOVE, width=20).place(x=20, y=80)
        Button(pencere, text="Open", relief=GROOVE, font="bold", command=self.UploadAction).place(x=230, y=80)
        self.Ent3 = Entry(pencere, width=24)
        self.Ent3.place(x=280, y=80)
        self.Ent3.insert(100, self.UploadAction())  

        Label(pencere, text="Enter key: ", relief=GROOVE, width=20).place(x=20, y=120)
        self.Ent2 = Entry(pencere, width=30)
        self.Ent2.place(x=230, y=120)

        Button(pencere, text="Encrypt", relief=GROOVE, font="bold", command=self.Encrypt).place(x=200, y=150)
        Button(pencere, text="Decrypt", relief=GROOVE, font="bold", command=self.Decrypt).place(x=280, y=150)

        Label(pencere, text="Result: ", relief=GROOVE, width=20).place(x=20, y=203)
        self.RESULT = Entry(pencere, width=30)
        self.RESULT.place(x=230, y=200)

    def Encrypt(self):
        key = int(self.Ent2.get())
        length = len(self.LETTERS)

        translation = ''

        text = self.Ent1.get()
        text = self.Ent3.get()
        text = re.sub('[^A-Za-z]+', '', text.lower())

        for character in text:
            if character in self.LETTERS:
                sayı = self.LETTERS.find(character)
                sayı = (sayı + key) % length
                translation += self.LETTERS[sayı]
            else:
                translation += character

        self.RESULT.delete(0, END)
        self.RESULT.insert(0, translation)

    def Decrypt(self):
        key = int(self.Ent2.get())
        length = len(self.LETTERS)

        translation = ''

        text = self.RESULT.get()
        text = re.sub('[^A-Za-z]+', '', text.lower())

        for character in text:
            if character in self.LETTERS:
                sayı = self.LETTERS.find(character)
                sayı = (sayı - key) % length
                translation += self.LETTERS[sayı]
            else:
                translation += character

        self.RESULT.delete(0, END)
        self.RESULT.insert(0, translation)

if __name__ == "__main__":
    root = Tk()
    root.title("Caesar")
    root.geometry("580x280+70+70")
    Caesar(root).pack(side="top", fill="both")
    root.mainloop()

至于我,问题是因为你使用

    self.Ent3.insert(100, self.UploadAction())   
\uuuu init\uuuuu
内部,所以它在开始时执行
UploadAction()
,而不是在按下按钮时执行

您必须使用按钮执行的
insert
内部
UploadAction()

def UploadAction(self):
    filename = filedialog.askopenfilename(initialdir="/",
                                      title="Open File",
                                      filetypes=(("Text Files", "*.txt"), ("All Files", "*.*")))

    if filename: 
        with open('caesartest.txt') as f:
            contents = f.read().splitlines()
            contents = ' '.join(map(str, contents))
        self.Ent3.insert(100, content) # <-- use it 

    # return contents # useless when used with `Button` 

def __init__(self, pencere):
    Frame.__init__(self, pencere)
    self.pencere = pencere

    Label(pencere, text="Enter your message: ", relief=GROOVE, width=20).place(x=20, y=30)
    self.Ent1 = Entry(pencere, width=30)
    self.Ent1.place(x=230, y=30)

    Label(pencere, text="Upload a .txt file: ", relief=GROOVE, width=20).place(x=20, y=80)
    Button(pencere, text="Open", relief=GROOVE, font="bold", command=self.UploadAction).place(x=230, y=80)
    self.Ent3 = Entry(pencere, width=24)

    self.Ent3.place(x=280, y=80) 

    # self.Ent3.insert(100, self.UploadAction()) # <-- don't do this
def上传操作(self):
filename=filedialog.askopenfilename(initialdir=“/”,
title=“打开文件”,
文件类型=((“文本文件”,“*.txt”),(“所有文件”,“**))
如果文件名为:
将open('caesatest.txt')作为f:
contents=f.read().splitlines()
contents=''.join(映射(str,contents))

self.Ent3.insert(100,content)#您如何处理self.Ent3.insert(100,self.UploadAction())
?您应该使用
self.Ent3.insert(…)
内部函数
UploadAction
只在单击按钮时插入文本。谢谢@furas的帮助。我现在有一个不同的问题。我已经更新了上面的文字说明和代码。非常感谢。若你们有新的问题,那个么你们应该在新的页面上创建新的问题。