无法创建图像按钮Python

时间:2018-06-26 22:52:15

标签: python user-interface tkinter

编辑: 我从此视频中找到了一种方法: https://www.youtube.com/watch?v=8HwHqa3tq70

但是对我来说,它不起作用,这是我的代码:

SELECT sum(power) AS total_power
FROM   foo
GROUP  BY grp

我得到的错误是这里:

from tkinter import *
from tkinter import ttk
rw = Tk()
b1 = ttk.Button(rw, text = "click")
b1.pack()

filepath = "2.jpg"
mi = PhotoImage(file = filepath)


b1.config(image = mi, compound = RIGHT)

对此有何建议?

1 个答案:

答案 0 :(得分:0)

好吧..您更改了问题。我将回答您先前的问题。希望对您有所帮助。

您可以使用PyQt5。 我有一个与此文件夹:

|-Project/
|--- images/
|----- 1.png
|------2.png
|--- app.py

有两个按钮,例如“ cam”图标和“ user”图标:

from PyQt5 import QtCore, QtGui, QtWidgets
import os

class Ui_Example(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(Ui_Example, self).__init__(parent)
        width = 350
        height = 180
        self.resize(width,height)
        self.setMaximumSize(width,height)
        self.setMinimumSize(width,height)
        self.setWindowTitle("Example")

        self.path = os.path.join(os.path.dirname(os.path.abspath(__file__)),'images') #icons path

        ### icons

        usericon = QtGui.QIcon()
        usericon.addPixmap(QtGui.QPixmap(os.path.join(self.path, '2.png')), QtGui.QIcon.Normal, QtGui.QIcon.Off)

        camicon = QtGui.QIcon()
        camicon.addPixmap(QtGui.QPixmap(os.path.join(self.path, '1.png')), QtGui.QIcon.Normal, QtGui.QIcon.Off)

        # Button user
        self.btnuser = QtWidgets.QPushButton(self)
        self.btnuser.setGeometry(QtCore.QRect(20,20,150,100))
        self.btnuser.setIcon(usericon)
        self.btnuser.setStyleSheet("background-color:transparent;")
        self.btnuser.clicked.connect(self.ExecUserCode)
        # Button cam
        self.btncam = QtWidgets.QPushButton(self)
        self.btncam.setGeometry(QtCore.QRect(180, 20, 150, 100))
        self.btncam.setIcon(camicon)
        self.btncam.setStyleSheet("background-color:transparent;")
        self.btncam.clicked.connect(self.ExecCamCode)
        # Button..
        # ...

    def ExecUserCode(self):
        print("User Code here")
    def ExecCamCode(self):
        print("Cam Code here")

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MW = Ui_Example()
    MW.show()
    sys.exit(app.exec_())

结果是:

enter image description here