繁体   English   中英

由于 label 的长度,Tkinter 移动 GUI

[英]Tkinter moving GUI because of length of label

我正在尝试制作一个根据文件类型和名称移动文件的简单程序。 我已经制作了所有的 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 看起来还不错。 label 更改和同一列中的其他 GUI 受到挤压。 这是照片。

没有任何文件位置:

我的 GUI 的照片

但是一旦我添加了文件位置,文本框就会移动,按钮也会移动。

我的 GUI 的照片

谢谢你的帮助。

使用columnspan上的.grid()的 columnspan 选项来扩展其内容。 acw1668

暂无
暂无

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

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