python - How to open another window and get data from it, then save to a file? -


i'm trying create program in python using tkinter, , program supposed have list of books created user. on main window (the 1 list), there should menubar option add book list. when clicked, option should open window, time 1 entrybox, user should enter book's title , add button, add button list.

the list saved in .txt file.

this program wrote far:

import sys tkinter import *  def newbook():     def add():         booktitle = v.get()         booktitle = '\n' + booktitle         books = open('c:/digitallibrary/books.txt', 'a')         books.write(booktitle)         books.close()      addwindow = tk()     v = stringvar()      addwindow.geometry('250x40+500+100')      addwindow.title('digitallibrary - add book')      newbookentry = entry(addwindow,textvariable=v)     newbookentry.pack()      addbutton = button(addwindow, text='add', command=add)     addbutton.pack()  def refresh():     books = open('c:/digitallibrary/books.txt', 'r')     booklist = books.readlines()     books.close()      in range (0, len(booklist)):         bookone = label(text=booklist[i])         bookone.grid(row=i, column=0, sticky=w)  def quitprogram():     tfquit = messagebox.askyesno(title='close program', message='are sure?')     if tfquit:         window.destroy()  window = tk() menubar = menu(window)  window.geometry('400x400+200+100')  window.title('digitallibrary')  booksmenu = menu(menubar, tearoff=0) booksmenu.add_command(label='add book', command=newbook) booksmenu.add_command(label='delete book') booksmenu.add_command(label='close program', command=quitprogram) menubar.add_cascade(label='digitallibrary', menu=booksmenu)  books = open('c:/digitallibrary/books.txt', 'r') booklist = books.readlines() books.close()  in range (0, len(booklist)):     bookone = label(window, text=booklist[i])     bookone.grid(row=i, column=0, sticky=w)   refreshbutton = button(window, text='refresh', command=refresh) refreshbutton.grid(row=0, column=1)  window.config(menu=menubar)  window.mainloop() 

it seems logical me should work, doesn't. when click add button on add book window, add line break .txt file. know works if use os library , create separate python file add book window, i'd rather put in 1 code, if possible. i've tried many things, , tried searching in web, got nowhere.

the root cause of problem creating more once instance of tk. cannot this. if want create popup window, create instance of toplevel. proper tkinter application creates once instance of tk 1 invocation of mainloop.

if main goal input user (versus learning how write own dialog), might want consider using 1 of built-in dialogs.

for example:

import tkinter.simpledialog tksimpledialog # python 3.x ... def newbook():     booktitle = tksimpledialog.askstring("add book","what name of book?")     if booktitle not none:         booktitle = '\n' + booktitle         books = open('/tmp/books.txt', 'a')         books.write(booktitle)         books.close() 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -