Ткинтер движется GUI из-за длины этикетки - PullRequest
0 голосов
/ 10 апреля 2020

Я пытаюсь создать простую программу, которая перемещает файлы в зависимости от типа файла и его имени. Я сделал все GUI. Вот код.

# 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()

GUI выглядит прекрасно, пока я не добавлю расположение файла. Метка изменится, и другие GUI в том же столбце будут сжаты. Вот фотографии.

Без указания местоположения файла:

Photo of my GUI

Но после добавления местоположения файла текстовое поле перемещается и кнопка тоже.

Photo of my GUI

Спасибо за любую помощь.

1 Ответ

0 голосов
/ 10 апреля 2020

Используйте columnspan параметр .grid() на ярлыке, чтобы расширить его содержимое. - acw1668

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...