簡體   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