Tkinter如何显示图像而不移动任何内容?

时间:2019-09-17 08:34:05

标签: python tkinter

这是我当前的Python代码:

from tkinter import *
import glob
import os
from PIL import Image, ImageTk
import tkinter as tk


root = tk.Tk()
root.title("SIGN OFF")
root.minsize(840, 800)

# Add a grid
mainframe = tk.Frame(root)
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)
mainframe.pack(pady=100, padx=100)

# Create a Tkinter variable
tkvar = tk.StringVar(root)

directory = "C:/Users/eduards/Desktop/work/data/to-do"
choices = glob.glob(os.path.join(directory, "*.jpg"))
tkvar.set('...To Sign Off...') # set the default option

popupMenu = tk.OptionMenu(mainframe, tkvar, *choices)
tk.Label(mainframe, text="Choose your sign off here:").grid(row=1, column=1)
popupMenu.grid(row=2, column=1)

label2 = tk.Label(mainframe, image=None)
label2.grid(row=4, column=0)

# On change dropdown callback.
def change_dropdown(*args):
    """ Updates label2 image. """
    imgpath = tkvar.get()
    img = Image.open(imgpath)
    img = img.resize((240,250))
    photo = ImageTk.PhotoImage(img)
    label2.image = photo
    label2.configure(image=photo)


tk.Button(mainframe, text="Open", command=change_dropdown).grid(row=3, column=1)

var1 = IntVar()
Checkbutton(mainframe, text="Ingredients present in full (any allergens in bold with allergen warning if necessary)", variable=var1).grid(column = 2, row=1, sticky=W)
var2 = IntVar()
Checkbutton(mainframe, text="May Contain Statement.", variable=var2).grid(column = 2, row=2, sticky=W)
var3 = IntVar()
Checkbutton(mainframe, text="Cocoa Content (%).", variable=var3).grid(column = 2, row=3, sticky=W)
var4 = IntVar()
Checkbutton(mainframe, text="Vegetable fat in addition to Cocoa butter", variable=var4).grid(column = 2, row=4, sticky=W)
var5 = IntVar()
Checkbutton(mainframe, text="Instructions for Use.", variable=var5).grid(column = 2, row=5, sticky=W)
var6 = IntVar()
Checkbutton(mainframe, text="Additional warning statements (pitt/stone, hyperactivity etc)", variable=var6).grid(column = 2, row=6, sticky=W)
var7 = IntVar()
Checkbutton(mainframe, text="Nutritional Information Visible", variable=var7).grid(column = 2, row=7, sticky=W)
var8 = IntVar()
Checkbutton(mainframe, text="Storage Conditions", variable=var8).grid(column = 2, row=8, sticky=W)
var9 = IntVar()
Checkbutton(mainframe, text="Best Before & Batch Information", variable=var9).grid(column = 2, row=9, sticky=W)
var10 = IntVar()
Checkbutton(mainframe, text="Net Weight & Correct Font Size.", variable=var10).grid(column = 2, row=10, sticky=W)
var11 = IntVar()
Checkbutton(mainframe, text="Barcode - Inner", variable=var11).grid(column = 2, row=11, sticky=W)
var12 = IntVar()
Checkbutton(mainframe, text="Address & contact details correct", variable=var12).grid(column = 2, row=12, sticky=W)


root.mainloop()

给出了我的输出:

enter image description here

我遇到的问题是,当我选择目录时,打开它扩展了所有其他内容的图像,我想知道如何将图像放置在复选框旁边而不移动复选框内容?

以下是输出:

enter image description here

这是我打算获得的输出(用于绘画的编辑):

enter image description here

2 个答案:

答案 0 :(得分:1)

您需要向rowspan变量中添加label2选项,如下所示:

label2.grid(row = 4, column = 0, rowspan = 10)

编辑rowspan值以获得所需的结果

答案 1 :(得分:0)

看起来您的窗口应该由两个独立的列组成。您可以通过嵌套Frame对象来做到这一点。从主框架开始,并排放置两个框架,如下所示:

mainframe = tk.Frame(root)
# configure your main frame here
leftframe = tk.Frame(mainframe)
leftframe.grid(row=0, column=0)
rightframe = tk.Frame(mainframe)
rightframe.grid(row=0, column=1)

现在您可以继续放置小部件了。例如:

# menu and label are supposed to be in the left column
tk.Label(leftframe, text="Choose your sign off here:").grid(row=0, column=0)
popupMenu = tk.OptionMenu(leftframe, tkvar, *choices)
# ...
# but you want the checkboxes on the right
var1 = IntVar()
Checkbutton(rightframe, text="Ingredients present in full (any allergens in bold with allergen warning if necessary)", variable=var1).grid(row=0, column=0, sticky=W)
# ...

请注意我对rowcolumn参数所做的更改。当然,正如爱德华(Eduard)所述,请阅读Grid Geometry Manual的文档。