简体   繁体   English

由于 label 的长度,Tkinter 移动 GUI

[英]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.我已经制作了所有的 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.在我添加文件位置之前,GUI 看起来还不错。 The label changes and other GUI in the same column get squeezed. label 更改和同一列中的其他 GUI 受到挤压。 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.使用columnspan上的.grid()的 columnspan 选项来扩展其内容。 acw1668 acw1668

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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