简体   繁体   English

显示文件名,而不显示文本小部件中的内容tkinter Python

[英]displaying file name, not content in text widget tkinter Python

I've no idea why I haven't been able to find a good solution to this problem yet, it seems very elementary to me .. though not elementary enough to figure it out satisfactorily. 我不知道为什么我还没有找到解决这个问题的好方法,这对我来说似乎很基础..尽管还不够基础,无法令人满意地解决它。 A chapter project in a cryptology book Im reading instructs to write a simple mono-alphabetic cipher in your preferred language ... I chose Python. 我正在阅读的一本密码学书籍中的一个章节项目指示我用您喜欢的语言编写一个简单的单字母密码。我选择了Python。

It starts with a simple tkinter app. 它从一个简单的tkinter应用程序开始。 with some widgets, lol ... duh. 还有一些小部件,哈哈... duh。 Anyways here's the relevant code: 无论如何,这里是相关的代码:

from tkinter import *
from tkinter.filedialog import askopenfilename
from tkinter.messagebox import showerror


class Application(Frame):
    def __init__(self, master):
        """ Initialize Frame. """
        super(Application, self).__init__(master)
        self.grid()
        self.create_widgets()

    def create_widgets(self):
        """ Set all program widgets. """
        # set all labels
        Label(self, text = "Plaintext File: ")\
            .grid(row=0, column=0, sticky=W)
        Label(self, text = "Ciphertext: ")\
            .grid(row=3, column=0, sticky=W)
        Label(self, text = "Offset: ")\
            .grid(row=2, column=0, sticky=W)

    # set buttons
    Button(self, text = "Browse", command=self.load_file, width=10)\
        .grid(row=1, column=0, sticky=W)

    # set entry field
    self.file_name = Text(self, width=39, height=1, wrap=WORD)
    self.file_name.grid(row=1, column=1, columnspan=4, sticky=W)

    # set display field
    self.output_display = Text(self, width=50, height=5, wrap=WORD)
    self.output_display.grid(row=4, column=0, columnspan=4, sticky=W)

    # set offset amount spinbox
    self.offset_amt = IntVar()

    self.offset_amt = Spinbox(self, from_=1, to=13)
    self.offset_amt.grid(row=2, column=1, sticky=W)

    # set shift direction
    self.shift_dir = StringVar()
    self.shift_dir.set('r')

    Radiobutton(self, text="Shift Right", variable=self.shift_dir, value='r')\
        .grid(row=2, column=2, sticky=W)
    Radiobutton(self, text="Shift Left", variable=self.shift_dir, value='l')\
        .grid(row=2, column=3, sticky=W)


def load_file(self):

    self.filename = askopenfilename(initialdir='~')


    if self.filename: 
        try: 
            #self.settings.set(self.filename)
            self.file_name.delete(0.0, END)
            self.file_name.insert(0.0, open(self.filename, 'r'))
        except IOError: 
            showerror("Open Source File", "Failed to read file \n'%s'"%self.filename)
            return


def main():
    root = Tk()
    root.title("simple mono-alpha encrypter")
    root.geometry('450x250')
    app = Application(root)

for child in app.winfo_children(): 
    child.grid_configure(padx=3, pady=3)

root.mainloop()

main()

There's only a very little of it that actually does anything besides create widgets right now, I decided to post it all since its not that involved yet and someone can get a good idea of where Im at. 除了创建窗口小部件之外,实际上几乎没有任何其他功能,我决定将其全部发布,因为还没有涉及到它,因此有人可以很好地了解Im的位置。

My problem that I haven't solved is that when I press use the 'Browse' button to choose a file to encrypt and then choose the file, the file content is displayed in the 'file_name' text widget, rather than the file name itself. 我尚未解决的问题是,当我按下“浏览”按钮选择要加密的文件然后选择文件时,文件内容显示在“ file_name”文本小部件中,而不是文件名本身。

Im thinking that I have to change the 'filename' variable to not the actual file name but the file instead and then load the content of the File Name field from the open file dialog box in a 'filename' variable. 我以为我必须将“文件名”变量更改为不是实际的文件名,而是文件,然后从“文件名”变量中的打开文件对话框中加载“文件名”字段的内容。 I just haven't been able to figure out how to do that yet. 我只是还无法弄清楚该怎么做。 Nor have I come across an appropriate method to do it. 我也没有遇到合适的方法来做到这一点。 Any guidance?? 有指导吗?

Thanks F 谢谢F

Displaying the Filename 显示文件名

self.file_name.insert(0.0, self.filename)

Displaying the File Contents 显示文件内容

You just need to read the data in from the file. 您只需要从文件中读取数据即可。 See http://docs.python.org/library/stdtypes.html#file-objects 参见http://docs.python.org/library/stdtypes.html#file-objects

with open(self.filename, 'r') as inp_file:
    self.file_name.insert(0.0, inp_file.read())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM