Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何更改按钮的颜色+;输入状态更改时更改输出状态_Python_User Interface_Tkinter_Button - Fatal编程技术网

Python 如何更改按钮的颜色+;输入状态更改时更改输出状态

Python 如何更改按钮的颜色+;输入状态更改时更改输出状态,python,user-interface,tkinter,button,Python,User Interface,Tkinter,Button,为了上学,我和特金特做了一个图形用户界面。现在我遇到了一个问题,我必须从GPIO端口扩展器(MCP2308)读取输入并使用该输入: 改变按钮的颜色 更改输出的状态 这听起来可能相当简单(一开始它对我来说太简单了),但当某些东西在不同的“def”函数中时,它对我来说太难了 这是我的代码设置: from tkinter import * import PIL as p import PIL.ImageTk as ptk import smbus import time #adafruit impo

为了上学,我和特金特做了一个图形用户界面。现在我遇到了一个问题,我必须从GPIO端口扩展器(MCP2308)读取输入并使用该输入:

  • 改变按钮的颜色
  • 更改输出的状态
  • 这听起来可能相当简单(一开始它对我来说太简单了),但当某些东西在不同的“def”函数中时,它对我来说太难了

    这是我的代码设置:

    from tkinter import *
    import PIL as p
    import PIL.ImageTk as ptk
    import smbus
    import time
    
    #adafruit
    import board
    import busio
    from digitalio import Direction
    from adafruit_mcp230xx.mcp23008 import MCP23008
    i2c = busio.I2C(board.SCL, board.SDA)
    mcp = MCP23008(i2c,address=0x21)
    import digitalio
    
    #setup i²c chips
    bus = smbus.SMBus(1)
    #MCP23017
    DEVICE = 0x20 #Device address (A0-A2)
    IODIRA = 0x00 #pin direction register
    IODIRB = 0x00
    OLATA  = 0x14 #register for outputs.
    OLATB  = 0x15
    GPIOA  = 0x12 #register for inputs
    GPIOB  = 0x13
    
    #set all GPA pins as outputs by setting
    #all bits of IODIRA register to 0
    bus.write_byte_data(DEVICE,IODIRA,0x00)
    
    #set output all 7 output bits to 0
    bus.write_byte_data(DEVICE,OLATA,0)
    
    #set all GPA pins as outputs by setting
    #all bits of IODIRB register to 0
    bus.write_byte_data(DEVICE,IODIRB,0x00)
    
    #set output all 7 output bits to 0
    bus.write_byte_data(DEVICE,OLATB,0)
    
    #MCP23008
    DEVICE2 = 0x21
    IODIR   = 0x00
    GPIO    = 0x09
    
    #set all GPA pins as inputs by setting
    #all bits of IODIRA register to 1
    bus.write_byte_data(DEVICE2,IODIR,0xFF)
    
    #all pins on mcp23008 are inputs
    pin0 = mcp.get_pin(0)
    pin0.direction = digitalio.Direction.INPUT
    
    
    
    global alpha
    alpha = 0
    
    #variables for 'relais' windwow
    global ledstate1
    ledstate1 = 0
    global ledstate2
    ledstate2 = 0
    global ledstate3
    ledstate3 = 0
    
    然后我做我的窗户和窗框

    #make mainwindow
    mainwindow=Tk()
    mainframe=Frame(mainwindow,bg='linen')
    mainframe.pack(fill='both', expand=True)
    mainwindow.geometry('800x480')
    mainwindow.resizable(False,False)
    mainwindow.attributes('-fullscreen',True)
    
    #make frame
    menu=Frame(mainframe,bg='linen',highlightbackground="black",highlightthickness=2)
    menu.pack(side='top',fill='x')
    
    这是我打开第二个窗口的“大”def函数

    #this is a def function where i open a second window with 9 buttons for my 9 outputs
    def open_relais():
        #window aanmaken
        win = Toplevel(bg='linen')          
        win.title('first window')
        win.geometry('800x480')
        win.resizable(False,False)
        win.attributes('-fullscreen',True)
        #frame maken
        relais = Frame(win,bg='linen')
        relais.pack()                       # put the frame at the top of the window
        #labels onderdelen aanmaken
        #welk onderdeel
        Label14=Label(relais, text='3 Fasen',bg='white',fg='black',borderwidth=2,relief='solid',font=('Helvetica',19,'bold'),padx=12,pady=5)
        Label15=Label(relais, text='1 Fase + aarding',bg='white',fg='black',borderwidth=2,relief='solid',font=('Helvetica',19,'bold'),padx=12,pady=5)
        Label16=Label(relais, text='1 Fase geïsoleerd',bg='white',fg='black',borderwidth=2,relief='solid',font=('Helvetica',19,'bold'),padx=12,pady=5)
    
        Label14.grid(column=1,row=0,padx=20,pady=15,sticky='new')
        Label15.grid(column=1,row=2,padx=20,pady=15,sticky='new')
        Label16.grid(column=1,row=4,padx=20,pady=15,sticky='new')
       
        #button 1 , together with Stop_bus_3F_1(Stop_bus_3F)
        Stop_bus_3F = Button(relais,text='Stopcontact + Bussen',command=lambda: Stop_bus_3F_1(Stop_bus_3F),fg='black',padx=10,pady=10)
        Stop_bus_3F.grid(column=1,row=1,padx=20,pady=12)
        Stop_bus_3F.config(bg="green" if ledstate1 else "red")
        #button 2 , together with Stop_3F_1(Stop_3F)
        Stop_3F = Button(relais,text='Stopcontact',command=lambda: Stop_3F_1(Stop_3F),fg='black',padx=10,pady=10)
        Stop_3F.grid(column=0,row=1,padx=20,pady=12)
        Stop_3F.config(bg="green" if ledstate2 else "red")
        #button 3, together with Bus_3F_1(Bus_3F)
        Bus_3F = Button(relais,text='Bussen',command=lambda: Bus_3F_1(Bus_3F),fg='black',padx=10,pady=10)
        Bus_3F.grid(column=2,row=1,padx=20,pady=12)
        Bus_3F.config(bg="green" if ledstate3 else "red")
    
        #close button second window
        close_button = Button(relais, text='close window', command=win.destroy).grid(column=1,row=6,padx=20,pady=15)
    
    然后我使用def功能打开/关闭LED

    # this are the def functions too turn the outputs on/off without changing other outputs
    # 3FASE
    def Stop_bus_3F_1(Stop_bus_3F):
        if ledstate2 == 0:                  
            if ledstate3 == 0:              
                global ledstate1
                global alpha
                ledstate1 = 1 - ledstate1
                if ledstate1 == 1:          
                    alpha = alpha | 1            
                else:
                    alpha = alpha & 254            
                print(alpha)
                Stop_bus_3F.config(bg="green" if ledstate1 else "red")
                bus.write_byte_data(DEVICE,OLATA,alpha)
    def Stop_3F_1(Stop_3F):
        if ledstate1 == 0:
            if ledstate3 == 0:
                global ledstate2
                global alpha
                ledstate2 = 1 - ledstate2
                if ledstate2 == 1:          
                    alpha = alpha | 2                
                else:
                    alpha = alpha & 253
                print(alpha)
                Stop_3F.config(bg="green" if ledstate2 else "red")
                bus.write_byte_data(DEVICE,OLATA,alpha)
    def Bus_3F_1(Bus_3F):
        if ledstate1 == 0:
            if ledstate2 == 0:
                global ledstate3
                global alpha
                ledstate3 = 1 - ledstate3
                if ledstate3 == 1:          
                    alpha = alpha | 4      
                else:
                    alpha = alpha & 251
                print(alpha)
                Bus_3F.config(bg="green" if ledstate3 else "red")
                bus.write_byte_data(DEVICE,OLATA,alpha)
    
    然后在这里我为我的一个输入投票,一个现在。 当引脚0的值为false(=按下实际按钮)时,应该会发生这种情况。 应该发生的是,“停止总线”按钮的颜色应该改变,其他按钮应该关闭。此外,还应打开/关闭“Stop_bus_3F”按钮控制的led

    #pollen for inputs
    def poll_for_data():
        if pin0.value == False:         # 3Fasen knop
            #something has to happen here probably
            print('button pressed')
        mainwindow.after(10, poll_for_data)
    
    最后是一些带有按钮的剩余代码

    #button for second window
    button1=Button(menu,text='Bedieningspaneel',command=open_relais,bg='grey89',fg='black',padx=10,pady=10)
    button1.grid(row=0,column=1,padx=36,pady=15,sticky='nsew')
    
    #button too close mainwindow
    button4=Button(menu,text='     close     ',command=mainwindow.destroy,bg='grey89',fg='black',padx=10,pady=10)
    button4.grid(row=0,column=3,padx=36,pady=15,sticky='e')
    
    #start the poll for the input
    mainwindow.after(300, poll_for_data)
    mainwindow.mainloop()
    

    我知道我的要求很高,但我已经试了几个小时了,但还是不能成功

    你能把你的代码缩减到一个新的版本吗?这也会有很大帮助。如果你能把你的问题缩小一点。据我所知,您的问题是更改按钮的颜色并模拟同时按下按钮,对吗?您可以使用
    .config(bg='color')
    重新配置某个小部件的背景颜色,在本例中为按钮。要按下按钮,可以使用
    .invoke()
    ,这也会触发回调