Created
May 26, 2019 12:24
-
-
Save dorosch/d374b138fe99c8642a150b066cba4f7e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from tkinter.filedialog import * | |
from tkinter import * | |
import shelve | |
def hello(): | |
information.insert(1.0, "Hello, this version v1.0...\n") | |
def save_dir(): #Считывание директории | |
global put | |
put=askdirectory() | |
entry_directory.insert('0', put) #Вставка директории в поле | |
return put | |
Wri={} | |
def read(): #Считывание и проверка ключа и значения | |
k=key.get() | |
v=value.get('1.0', END+'-1c') | |
Wri[k]=v | |
if len(k)<1: | |
cmd(2) | |
if len(v)<1: | |
cmd(3) | |
if len(k)<1 and len(v)<1: | |
cmd(4) | |
else: | |
cmd(5) | |
return Wri | |
def creat(): #создание и сохранение файла с выводом в командную строку | |
fname=entry_namedir.get() | |
try: | |
dbase=shelve.open('%s\\%s' % (put, fname)) | |
except: | |
cmd(1) | |
else: | |
cmd(0) | |
for(x,y) in Wri.items(): | |
dbase[x]=y | |
dbase.close() | |
def cmd(x): #Командная строка | |
a='create-> ' | |
e='error-> ' | |
d='delete-> ' | |
w='write-> ' | |
k=key.get() | |
fname=entry_namedir.get() | |
if x==0: | |
key.delete('0', END) | |
value.delete('1.0', END) | |
information.insert(1.0, "%sФайл с именем '%s' был успешно создан и сохранён в директории '%s'\n" % (a, fname, put)) | |
if x==1: | |
information.insert(1.0, "%sНе указанна директория для сохранения файла '%s'\n" % (e, fname)) | |
if x==2: | |
key.delete('0', END) | |
information.insert('1.0', "%sСлишком короткий ключ, запись невозможна!\n" % (e)) | |
if x==3: | |
key.delete('0', END) | |
value.delete('1.0', END) | |
information.insert('1.0', "%sСлишком короткое значение, запись невозможна!\n" % (e)) | |
if x==4: | |
key.delete('0', END) | |
value.delete('1.0', END) | |
information.insert('1.0', "%sСлишком короткий ключ и значение!\n" % (e)) | |
if x==5: | |
key.delete('0', END) | |
value.delete('1.0', END) | |
information.insert('1.0', "%sБыла произведенна запись в файл '%s' по ключю '%s'!\n" % (w, fname, k)) | |
root=Tk() | |
root.title("GUI shelve") | |
############################################################################# | |
Info=Frame() # # | |
Info.pack(side=BOTTOM, fill=X) #Иммитация командной # | |
information=Text(Info, height=4, font="8", wrap=WORD) # строки # | |
information.pack(fill=X) # # | |
############################################################################# | |
Left=Frame() #Один из главных фреймов | |
Left.pack(side=LEFT, anchor=NW, fill=Y) | |
header=Frame(Left) | |
header.pack() | |
Label(header, text="Создание и сохранение модуля:\n").pack() | |
werch=Frame(Left) | |
werch.pack(anchor=W) | |
Label(werch, text="Введите имя сохраняемого файла:").pack(side=LEFT) | |
entry_namedir=Entry(werch) | |
entry_namedir.insert(0, "TestDB") #Для вызова .get() | |
entry_namedir.pack(side=LEFT) | |
direct=Frame(Left) | |
direct.pack(anchor=W) | |
Label(direct, text="Выберите директорию для сохранения:").pack(side=LEFT) | |
but_dir=Frame(Left) | |
but_dir.pack(anchor=W) | |
Button(but_dir, text="Выбрать", command=save_dir).pack(side=LEFT) | |
entry_directory=Entry(but_dir, width=47) #Для вызова .get() | |
entry_directory.pack(side=LEFT) | |
frkey=Frame(Left) | |
frkey.pack(anchor=W) | |
Label(frkey, text="Введите ключ для сохранения данных:").pack(side=LEFT) | |
key=Entry(frkey) #Для вызова .get() | |
key.pack(side=LEFT) | |
ftext=Frame(Left) | |
ftext.pack() | |
Label(ftext, text="Введите значение для ключа:").pack(anchor=W) | |
value=Text(ftext, width=40, height=15, font="Verdana 12", wrap=WORD) | |
value.pack(anchor=W) #Для вызова .get() | |
but_ww=Frame(Left) | |
but_ww.pack(anchor=W, fill=X) | |
Button(but_ww, text="\nЗаписать значение\n", command=read).pack(side=LEFT) | |
Button(but_ww, text="\nСоздать файл\n", command=creat).pack(side=RIGHT) | |
hello() | |
root.mainloop() | |
if __name__=='__main__': #Тестирование | |
print(Wri) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment