我在网上找到了下面的代码,并尝试将其调整为适合我的个人需求的代码,但出现错误: tkinter.TclError:无法在其中使用几何图形管理器网格。其中已经有由包管理的奴隶。
这是我在网上找到的代码,可通过单击小图像来显示大图像:
import tkinter as tk
nb_rows = 10
images = (
("images/small_images/small_image1.png", "images/big_images/big_image1.png"),
("images/small_images/small_image2.png", "images/big_images/big_image2.png"),
("images/small_images/small_image3.png", "images/big_images/big_image3.png"),
("images/small_images/small_image4.png", "images/big_images/big_image4.png"),
# ...
)
def visualize(event):
event.widget.unbind("<Button-1>")
window = tk.Toplevel(event.widget)
window._image = tk.PhotoImage(file=event.widget._filenames[1])
tk.Label(window, image=window._image).pack()
window.protocol("WM_DELETE_WINDOW", lambda: on_close(window))
def on_close(window):
window.master.bind("<Button-1>", visualize)
window.destroy()
root = tk.Tk()
labels, row, column = [], 0, -1
for index, filenames in enumerate(images):
label = tk.Label(root)
label["image"] = label._image = tk.PhotoImage(file=filenames[0])
label._filenames = filenames
label.bind("<Button-1>", visualize)
if not index % nb_rows:
column, row = 0, row + 1
label.grid(column=column, row=row)
row += 1
labels.append(label)
root.mainloop()
这是我的代码的一部分,可以正常工作,我想将上面的代码集成到左侧(文本左侧),并使其适应功能:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from tkinter import *
root=Tk()
root.iconbitmap(r'images/cev.ico')
text_part1_loc35R="\n===============================\n"
text_part2_loc35R="Emetteur Directif 1 :\n"
text_part3_loc35R="===============================\n"+\
"Puissance directe (P+BL) : 4.47 w\n"+\
"Puissance reflechie (P+BL) : 0.13 w\n"+\
"Puissance directe (BLS) : 0.09 w\n"+\
"Puissance reflechie (BLS) : 0.00 w\n"+\
"===============================\n"
text_part4_loc35R="Emetteur Clearance 1 :\n"
text_part5_loc35R="===============================\n"+\
"Puissance directe (P+BL) : 0.55 w\n"+\
"Puissance reflechie (P+BL) : 0.09 w\n"+\
"Puissance directe (BLS) : N/A w\n"+\
"Puissance reflechie (BLS) : N/A w\n"+\
"###############################\n\n"
generalites = Frame(root,bg="powder blue",width=750,height=250)
generalites.pack()
##########################################
#les sous frames de generalites(top,bottom):
generalites_top=Frame(generalites,relief=RIDGE,bd=18,bg='cadet blue')
generalites_top.pack(side=TOP,fill=X)
titre_generalites_top=Label(generalites_top,text='Generalites Techniques LOC 35R',font=('arial',22,'bold'),bg='cadet blue',padx=120)
titre_generalites_top.pack()
###############################################
generalites_bottom=Frame(generalites,bg='powder blue',width=750,height=270)
generalites_bottom.pack(side=BOTTOM)
#################################################
generalites_bottom_left=Frame(generalites_bottom,bg='powder blue',width=375)
generalites_bottom_left.pack(side=LEFT)
generalites_bottom_right=Frame(generalites_bottom,bg='powder blue',width=375)
generalites_bottom_right.pack(side=LEFT)
##################################################
T_generalites_left = Text(generalites_bottom_left,padx=20, height=26, width=30)
s_generalites_left = Scrollbar(generalites_bottom_left, command=T_generalites_left.yview)
T_generalites_left.configure(yscrollcommand=s_generalites_left.set)
T_generalites_left.tag_configure('style_loc35R',font=('arial',12,'bold'),foreground='blue',justify='center')
photo1=PhotoImage(file='images/test/image1.png')
photo2=PhotoImage(file='images/test/image2.png')
photo3=PhotoImage(file='images/test/image3.png')
photo4=PhotoImage(file='images/test/image4.png')
photo5=PhotoImage(file='images/test/image5.png')
for (j,k) in [('\nphoto 1\n',photo1),('\n\nphoto 2\n',photo2),('\n\nphoto 3\n',photo3),('\n\nphoto 4\n',photo4),('\n\nphoto 5\n',photo5)]:
T_generalites_left.insert(END,j,'style_loc35R')
T_generalites_left.image_create(END, image=k)
T_generalites_left.insert(END,'\n\n')
T_generalites_left.pack(side=LEFT,anchor=NW)
s_generalites_left.pack(side=LEFT, fill=Y)
T_generalites_right = Text(generalites_bottom_right, bg='powder blue',font=('arial',14,'bold'),padx=15, height=18, width=52)
s_generalites_right = Scrollbar(generalites_bottom_right, command=T_generalites_right.yview)
T_generalites_right.configure(yscrollcommand=s_generalites_right.set)
T_generalites_right.tag_configure('style_loc35R', foreground='blue',justify='center',font=('arial', 12, 'bold'))
T_generalites_right.insert(END,text_part1_loc35R)
T_generalites_right.insert(END,text_part2_loc35R,'style_loc35R')
T_generalites_right.insert(END,text_part3_loc35R)
T_generalites_right.insert(END,text_part4_loc35R,'style_loc35R')
T_generalites_right.insert(END,text_part5_loc35R)
T_generalites_right.pack(side=LEFT)
s_generalites_right.pack(side=RIGHT, fill=Y)
root.mainloop()
感谢帮助