【问题标题】:Upload image in tkinter grid在 tkinter 网格中上传图像
【发布时间】:2020-03-11 13:21:14
【问题描述】:

尝试将我的父窗口拆分为几个功能部分,其中之一将处理天气 数据等和另一个将位于右侧并包含地图图像,最好在全高延伸。虽然图像不想出现..请帮助

import pyowm
from tkinter import *
import tkinter.messagebox
from PIL import Image, ImageTk

owm = pyowm.OWM('....')  # You MUST provide a valid API key
class MyWindow(tkinter.Frame):
    def __init__(self, win):
        super(MyWindow, self).__init__()
        self.lbl=Label(win, text="Weather info", fg='black', font=("Helvetica", 11))
        self.lbl1=Label(win, text='Wind speed')
        self.lbl2=Label(win, text='Wind direction')

        self.lbl.grid(row = 0, column = 0, sticky =  W, pady = 2) 
        self.lbl1.grid(row = 1, column = 0, sticky = W, pady = 2)
        self.lbl2.grid(row = 2, column = 0, sticky = W, pady = 2)

        # widgets for destination and weather output
        self.e1=Entry()
        self.e2=Entry()
        self.e3=Entry(bd=3)

        # this will arrange entry widgets 
        self.e1.grid(row = 0, column = 1, pady = 2) 
        self.e2.grid(row = 1, column = 1, pady = 2)
        self.e3.grid(row = 2, column = 1, pady = 2)

        self.btn1 = Button(win, text='Get weather', command=self.getWeather)
        self.btn1.grid(row = 3, column = 1, pady = 2)

        self.btn1.bind('<Button-1>', self.getWeather)

        img = Image.open(r"/User/.../pic.png")
        photo = ImageTk.PhotoImage(img) 

        # setting image with the help of label 
        self.imgLBL = Label(self, image = photo)
        self.imgLBL.grid(row = 0, column = 2, columnspan = 2, rowspan = 2, padx = 5, pady = 5)
        self.imgLBL.image = photo

    #### Get weather function, etc...###

window=Tk()
mywin=MyWindow(window)
window.title('name')
window.geometry("2732x2048")
window.configure(bg='grey')
window.mainloop()

【问题讨论】:

标签: python image tkinter grid tkinter-layout


【解决方案1】:

将最后几行替换为:

self.photo = ImageTk.PhotoImage(Image.open(r"/User/.../pic.png"))

# setting image with the help of label
self.imgLBL = Label(window, image=self.photo)
self.imgLBL.grid(row = 0, column = 2, columnspan = 2, rowspan = 2, padx = 5, pady = 5)
self.imgLBL.image = self.photo

【讨论】:

  • 非常感谢!图像现在在那里,只是它出现在窗口的中心并挤压了左侧的所有内容,现在只有 1 个条目窗口可见并且它被推到了中间..
  • @Olexiy Usov 您需要重新排列元素的位置,或者为图像设置几行。
  • 谢谢!是的,我正在重新安排元素。
猜你喜欢
  • 1970-01-01
  • 2013-03-29
  • 2022-01-05
  • 2016-12-17
  • 2021-12-23
  • 2017-07-24
  • 1970-01-01
  • 2019-01-20
  • 2020-10-07
相关资源
最近更新 更多