Python:如何从变量设置为Entry对象的值

时间:2018-11-27 20:04:19

标签: python python-3.x

我尝试构建微型应用程序,在该应用程序中我将图像设置为应用程序。是可行的,但我对一件事有疑问。我尝试使用函数easy.fileopenbox获取文件路径,我需要将其打印到入口对象。在我的完整代码下面,注释“ <-----”显示了讨论的主题。 也许这很简单,但是我是Python新手。谢谢!

from tkinter.ttk import Frame, Style, Label, Entry, Button, Combobox
from tkinter import BOTH, Tk, W, E, N, S, Canvas, NW
from PIL import Image, ImageTk
import easygui

max_h=500
max_w=900

class Okno(Frame):
    def __init__(self, parent):
        super().__init__(parent)
        self.parent=parent
        self.inicjalizuj()
    def wczytaj_ponownie(self):
        self.image=ImageTk.PhotoImage(self.im)
        self.podst.create_image(0,0, image=self.image, anchor=NW)
    def wczytaj_obraz(self):
        sciezka=easygui.fileopenbox(msg=('Wskaż odpowiedni plik'), default="C:/Users/Darkous/Desktop/tutorial/IMAGE/*.jpg")  ##self.o.get()
        print(sciezka)
        self.o=sciezka                          ## -- here i try set value from sciezka to object
        self.im=Image.open(sciezka)
        self.fbtn.config(state='normal')
        self.zbtn.config(state='normal')
        self.sbtn.config(state='normal')
        self.pbtn.config(state='normal')
        self.obraz_oryg=self.im
        self.wczytaj_ponownie()

    def zapisz(self):
        sciezka=self.z.get()
        if sciezka=='':
            sciezka=self.o.get()
        self.im.save(sciezka)

    def inicjalizuj(self):
        self.parent.title("Kurs Pythona")
        self.styl=Style()
        self.styl.theme_use("winnative")
        self.pack(fill=BOTH,expand=1)
        self.columnconfigure(1,weight=1)
        etykieta=Label(self, text="Ścieżka do pliku:")
        etykieta.grid(sticky=W, pady=4, padx=5)

        self.o=Entry(self)                              ## I inicialize object <--------------------------------
        self.o.grid(row=1, column=0, columnspan=2, rowspan=1, padx=5, pady=4, sticky=E+W+S+N)

        self.z=Entry(self)
        self.z.grid(row=2, column=0, columnspan=2, rowspan=1, padx=5, pady=4, sticky=E+W+S+N)

        otbtn=Button(self, text="Otwórz", command=self.wczytaj_obraz)
        otbtn.grid(row=1,column=3)

        self.zbtn=Button(self, text="Zapisz", command=self.zapisz)
        self.zbtn.grid(row=2,column=3)
        self.zbtn.config(state="disabled")

        self.sxbox=Combobox(self, values='0.1 0.2 0.3 0.4')
        self.sxbox.grid(row=3, column=0, pady=4, padx=5, sticky=W+N)

        self.fcbox=Combobox(self, values='BLUR CONTOUR EMBOSS')
        self.fcbox.grid(row=4, column=0, pady=4, padx=5, sticky=W+N)

        self.sbtn=Button(self, text="Skaluj")
        self.sbtn.grid(row=3,column=1, pady=4, padx=5, sticky=W+N)
        self.sbtn.config(state="disabled")

        self.fbtn=Button(self, text="Filtruj")
        self.fbtn.grid(row=4,column=1, pady=4, padx=5, sticky=W+N)
        self.fbtn.config(state="disabled")

        self.podst=Canvas(self, width=max_h, height=max_h)
        self.podst.grid(row=5, column=0, pady=4, padx=5, sticky=E+W+S+N, columnspan=3)

        self.pbtn=Button(self, text="Przywróć")
        self.pbtn.grid(row=5,column=3, pady=4, padx=5, sticky=W+N)
        self.pbtn.config(state="disabled")

def main():
    gui=Tk()
    gui.geometry("1000x700")
    app=Okno(gui)
    gui.mainloop()

if __name__ == '__main__':
    main()

1 个答案:

答案 0 :(得分:1)

更改您的self.o=sciezka,使其显示为

self.o.delete(0, END)
self.o.insert(0, sciezka)

第一行删除Entry内的任何文本,第二行将您的文本插入小部件。两者中的0表示您要从条目的开头开始。

还将END添加到from tkinter import BOTH, Tk, W, E, N, S, Canvas, NW,使其读取from tkinter import BOTH, Tk, W, E, N, S, Canvas, NW, END