简体   繁体   中英

Tkinter moving GUI because of length of label

I am trying to make a simple program that moves files depending on its file type and its name. I have made all the GUI. Heres the code.

# Imports
from pathlib import Path
from os import path
import json
from tkinter import *
from tkinter import filedialog
# Imports

file = ""
file_location = ""


def downloads_folder() -> str: # Returnes the users Download folder
    return str(path.join(Path.home(), "Downloads"))

def tkinter_init(r): # Makes GUI for Tkinter window
    r.title("PyFM")
    r.resizable(False, False)

    # Initialize GUI
    l = Label(r, text = "Enter file type or file name: ")
    fileInput = Entry(r)
    text = StringVar()
    fileLocationLabel = Label(r, textvariable=text)
    text.set("File Location: None")
    fileLocation = Button(r, text = "Add File Location", command = lambda: add_file_location(text))
    done = Button(r, text = "Done", command = lambda: doneFunc())

    # Adds GUI to screen
    l.grid(row = 0, column = 0, sticky = "w")
    fileInput.grid(row = 0, column = 1, sticky = "w")
    fileLocation.grid(row = 1, column = 0, sticky = "w")
    fileLocationLabel.grid(row = 2, column = 0, sticky = "w")
    done.grid(row = 3, column = 1, sticky = "e")

def add_file_location(stringVar): # Makes a file lcoation popup and changes varible file_location to the file location.
    print("Add file location clicked")
    file_location = filedialog.askdirectory()
    stringVar.set(f"File Location: {file_location}")
    print(f"File location: {file_location}")

def doneFunc():
    print("Done button clicked")


if __name__ == '__main__':
    f = open("data.json", "r")
    data = json.loads(f.read())
    r = Tk()
    tkinter_init(r)
    r.mainloop()

The GUI looks just fine until I add a file location. The label changes and other GUI in the same column get squeezed. Here are the photos.

Without any file location:

我的 GUI 的照片

But once I add a file location, the text box gets moved and so does the button.

我的 GUI 的照片

Thanks for any help.

Use columnspan option of the .grid() on the label to expand its content. acw1668

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM