0

I have two functions and two GUI. I cannot get them to work after each other. first, I want to open my browse GUI then editing GUI. help me out, please.

from tkinter import *
from tkinter import filedialog
window = Tk()
def fileme():
    root = Tk()
    root.withdraw()
    file_path = filedialog.askopenfilenames(filetypes=[("Text file","*.txt")])
    print(file_path)
    window.withdraw()
    with open(file_path[0]) as f:
        f_contents = f.read()
        b=(f_contents)
        print(b)
window.title('Edit and save text files by Ali')
frame = Frame(window)
btn = Button(frame, text = 'Browse', command= fileme)
btn.pack(side = RIGHT , padx =55)
frame.pack(padx=100,pady = 30)

root=Tk()
x=filedialog.askopenfilename(filetypes=[("Text file","*.txt")])
T=Text(root,state='normal', height=20, width=70)
T.pack()
T.insert(END, open(x).read())
def save():
    b = T.get('1.0', END)
    f = open(x, 'wt')
    f.write(b)
    f.close()
btn= Button(root, text='Save', command=save)
btn.pack(side = RIGHT , padx =55)
window.mainloop()
root.mainloop()
saeed foroughi
  • 1,662
  • 1
  • 13
  • 25
Epiphnac
  • 1
  • 2
  • Where is `browse` and `edit` in your gui? – J Arun Mani Jan 19 '20 at 10:09
  • browse is by function fileme and edit =x which has the function save at the end / to say it in another word: I created a browse gui and a text gui , these two should work together first i need to browse the files using a button then open my text files in order to edit them. finally with a (save) button it would be done. – Epiphnac Jan 19 '20 at 10:15
  • 2
    Your objective seems confusing – J Arun Mani Jan 19 '20 at 10:17
  • 2
    Read [Why are multiple instances of Tk discouraged?](https://stackoverflow.com/questions/48045401/why-are-multiple-instances-of-tk-discouraged) – stovfl Jan 19 '20 at 12:36
  • You should use `Tk()` only once. For creating more windows you should use `Toplevel`. – DaniyalAhmadSE Jan 19 '20 at 14:54
  • You should not use `Tk()` more than once if you need more than one window then use `Toplevel()` – DaniyalAhmadSE Jan 19 '20 at 15:13

2 Answers2

0

I figured it out Here is an answer to this type of questions. hope to be useful for anyone who is learning programming.

from tkinter import *
from tkinter import filedialog
window=Tk()
def x():
    x=filedialog.askopenfilename(filetypes=[("Text file","*.txt")])
    T = Text(window, state='normal', height=20, width=70)
    T.pack()
    T.insert(END, open(x).read())

    def save():
        b = T.get('1.0', END)
        f = open(x, 'wt')
        f.write(b)
        f.close()
    btn1 = Button(window, text='Save', command=save)
    btn1.pack(side=RIGHT, padx=55)
window.title('Edit and save text files by Ali')
frame = Frame(window)
btn = Button(frame, text = 'Browse', command= x)
btn.pack(side = RIGHT , padx =55)
frame.pack(padx=100,pady = 30)

window.mainloop()
Epiphnac
  • 1
  • 2
0

@Epiphnac In your solution both the browser window and the text editor open in the same window. I think my solution is a little bit better than that.

I have put the editor inside a new window using Toplevel. Like this:

import tkinter as tk
from tkinter import filedialog

window = tk.Tk()


def editor():
    x = filedialog.askopenfilename(filetypes=[("Text file", "*.txt")])
    new_window = tk.Toplevel()
    t = tk.Text(new_window, state='normal', height=20, width=70)
    t.pack()
    t.insert(tk.END, open(x).read())

    def save():
        b = t.get('1.0', tk.END)
        f = open(x, 'wt')
        f.write(b)
        f.close()
    btn1 = tk.Button(new_window, text='Save', command=save)
    btn1.pack(side=tk.RIGHT, padx=55)


window.title('Edit and save text files by Ali')
frame = tk.Frame(window)
btn = tk.Button(frame, text='Browse', command=editor)
btn.pack(side=tk.RIGHT, padx=55)
frame.pack(padx=100, pady=30)

window.mainloop()
DaniyalAhmadSE
  • 807
  • 11
  • 20