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

发布于 2025-01-08 10:51:50 字数 2727 浏览 0 评论 0原文

我不知道为什么我还没有找到解决这个问题的好方法,这对我来说似乎非常基本……尽管还不够基本,无法令人满意地解决这个问题。 我正在阅读的密码学书中的一个章节项目指示用您喜欢的语言编写一个简单的单字母密码......我选择了Python。

它从一个简单的 tkinter 应用程序开始。与一些小部件,哈哈...废话。不管怎样,这里是相关的代码:

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()

除了现在创建小部件之外,实际上只有很少的一部分可以做任何事情,我决定将其全部发布,因为它还没有涉及到,并且有人可以很好地了解我在哪里。

我尚未解决的问题是,当我按下“浏览”按钮选择要加密的文件,然后选择该文件时,文件内容显示在“文件名”文本小部件中,而不是文件名本身。

我认为我必须将“文件名”变量更改为文件而不是实际文件名,然后从“文件名”变量中的打开文件对话框加载“文件名”字段的内容。我只是还不知道该怎么做。 我也没有找到合适的方法来做到这一点。 有什么指导吗??

谢谢 F

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.

It starts with a simple tkinter app. with some widgets, lol ... 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.

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

淡看悲欢离合 2025-01-15 10:51:50

显示文件名

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

显示文件内容

您只需从文件中读取数据即可。请参阅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())

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

with open(self.filename, 'r') as inp_file:
    self.file_name.insert(0.0, inp_file.read())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文