如何在 tkinter 中显示背景图像和按钮? [英] How to have an background image and buttons on it in tkinter?

查看:56
本文介绍了如何在 tkinter 中显示背景图像和按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 tkinter 编写一个简单的 Python 3 程序.它应该显示一个背景图片和一个按钮.

I'm writing a simple Python 3 program using tkinter. it should display a background picture and a button.

代码如下:

import tkinter
from PIL import Image
from PIL import ImageTk

window = tkinter.Tk()
file = Image.open('/Users/dariushmazlumi/Desktop/p.jpg')
img = ImageTk.PhotoImage(file)
background = tkinter.Label(window, image=img)
background.image = img
background.pack()
window.minsize(height=window.winfo_height(), width=window.winfo_width())
number = 0
def buttonclicked():
    global number
    number = number+1
    button.configure(text=number)
button = tkinter.Button(window, text=0, command=buttonclicked)
button.grid(column=1, row=1)
window.mainloop()

在此之前,我尝试使用 button.pack(),但它在图像下方显示按钮,而不是在其上(可能图像不是背景).

Prior to this, I tried using button.pack(), but it shows button under the image, not on it(maybe the image is not background).

接下来,我尝试使用 button.grid().它在终端上运行没有错误,但没有可见的输出!它只是运行.我不知道为什么.

Next, I tried using button.grid(). It runs on the terminal with no error, but no visible output! It just runs. I don't know why.

我希望我的程序在其上显示图像和按钮(例如桌面).

I want my program to display an image and buttons on it (like a desktop for example).

推荐答案

我找到了一种简单的方法来做你想做的事情,它比我在评论中建议的要简单得多.基本步骤是:创建一个tkinter.Canvas,用Canvas.create_image()在其上显示图像,然后创建一个Canvas.create_window()code>,最后将 tkinter.Button 放在 that 中.请注意,每个Canvas窗口"只能容纳一个小部件,因此如果您想在图像上放置多个按钮,则必须重复最后个步骤.

I found a simple way to do what you want which is much less complex that what I was suggesting in my comments. The essential steps are: Create a tkinter.Canvas, display the image on it with Canvas.create_image(), next create a Canvas.create_window() and finally put the tkinter.Button in that. Note that each Canvas "window" can only hold one widget, so you'll have to repeat the last two steps if you want to put more than one button on the image.

查看下面的代码可能更容易理解:

It may be simpler to understand by checking out the code below:

import tkinter as tk
from PIL import ImageTk, Image

class CanvasButton:
    def __init__(self, canvas):
        self.canvas = canvas
        self.number = tk.IntVar()
        self.button = tk.Button(canvas, textvariable=self.number,
                                command=self.buttonclicked)
        self.id = canvas.create_window(50, 100, width=25, height=25,
                                       window=self.button)
    def buttonclicked(self):
        self.number.set(self.number.get()+1)  # auto updates Button

root = tk.Tk()
root.resizable(width=False, height=False)
root.wm_attributes("-topmost", 1)

imgpath = 'archipelago_big.gif'
img = Image.open(imgpath)
photo = ImageTk.PhotoImage(img)

canvas = tk.Canvas(root, bd=0, highlightthickness=0)
canvas.pack()
canvas.create_image(0, 0, image=photo)

CanvasButton(canvas)  # create a clickable button on the canvas

root.mainloop()

这是点击几次按钮后的样子:

Here's what it looks like after clicking on the button a few times:

这篇关于如何在 tkinter 中显示背景图像和按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆