#๐Ÿ”’ Im a bit new, problems with Tkinter

4 messages ยท Page 1 of 1 (latest)

crystal leaf
#

I made a simple text editor and have no idea how to make its window to open full-screen and when you maximize it, the editor stays the same size. Pictures and code below. How can I make the app run maximized from the start and the actual text editor covering most of it? Thanks.

from tkinter.filedialog import askopenfilename, asksaveasfilename

def save_file(window, text_edit):
    filepath = asksaveasfilename(filetypes=[("Text Files", "*.txt")])
    
    if not filepath:
        return
    
    with open(filepath, "w") as f:
        content = text_edit.get(1.0, tk.END)
        f.write(content)
        window.title(f"Open File: {filepath}")
        
def open_file(window, text_edit):
        filepath = askopenfilename(filetypes=[("Text Files", "*.txt")])
    
        if not filepath:
            return
    
        text_edit.delete(1.0,tk.END)
        with open(filepath, "r") as f:
            content = f.read()
            text_edit.insert(tk.END, content)
        window.title(f"Open File: {filepath}")



def main():
    window = tk.Tk()
    window.title("Text Editor")
    window.rowconfigure(0, minsize=400)
    window.columnconfigure(1, minsize=500)
    
    text_edit = tk.Text(window)
    text_edit.grid(row=0, column=1)
    
    frame = tk.Frame(window, relief=tk.RAISED, bd=2)
    save_button = tk.Button(frame, text="Save", command=lambda: save_file(window, text_edit))
    open_button = tk.Button(frame, text="Open", command=lambda: open_file(window, text_edit))
    save_button.grid(row=0, column=0, padx=5, pady=5, sticky="ew")
    open_button.grid(row=1, column=0, padx=5, sticky="ew")
    frame.grid(row=0, column=0, sticky="ns")
    
    window.mainloop()

    window.bind("<Control-s>", lambda x: save_file(window, text_edit))
    window.bind("<Control-o>", lambda: open_file(window, text_edit))
    
main() ```
surreal pythonBOT
#

@crystal leaf

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

surreal pythonBOT
#

@crystal leaf

Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.