点击按钮实现图片的切换用tkinter能实现吗?应该怎么做

2021-04-08 10:37发布

2条回答
aijingda
2楼 · 2021-04-09 10:18

直接给你上代码~

from tkinter import *

class i_helper:
    def __init__(self,master):
        self.master = master
        self.initWidgets()
        
    def initWidgets(self):
        self.img = Label(self.master, width = 120,height = 60, bg='white')
        src = PhotoImage(file = '../img/switch_off.gif')
        self.img.x = src
        self.img['image'] = src
        self.img.state = "on"
        self.img.bind('',self.switch_change)
        self.img.pack()
        
    def switch_change(self,event):
        if self.img.state == "on":
            self.switch_off(event)
        else:
            self.switch_on(event)
    
    def switch_on(self,event):
        src = PhotoImage(file = '../img/switch_on.gif')
        self.img.x = src
        self.img['image'] = src
        self.img.pack()
        self.img.state = "on"
        
    def switch_off(self,event):
        src = PhotoImage(file = '../img/switch_off.gif')
        self.img.x = src
        self.img['image'] = src
        self.img.pack()
        self.img.state = "off"

root = Tk()

root.title('mojito')

i_helper(root)

root.mainloop()

效果↓↓↓

switch_on
switch_off

import android.app.Activity;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.ImageView;


public class MainActivity extends Activity implements OnClickListener {

    private ImageView mImage;

    private Button mBut1, mBut2;

    private int Imagess[] = { R.drawable.guide_1, R.drawable.guide_2, R.drawable.guide_3, R.drawable.guide_4,

            R.drawable.guide_5 };

    private int index =0;// 存放当前图片的索引


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        initView();

    }


    private void initView() {

        mImage = (ImageView) findViewById(R.id.lv1);

        mBut1 = (Button) findViewById(R.id.ButLeft);

        mBut2 = (Button) findViewById(R.id.ButRight);

        selectImage();

        mBut1.setOnClickListener(this);

        mBut2.setOnClickListener(this);


    }


    @Override

    public void onClick(View v) {

        // TODO Auto-generated method stub

        int ID = v.getId();

        if (R.id.ButLeft == ID) {

            index-= 1;

            if (index <0>

                index = Imagess.length-1;


        }else {

            index+=1;


            if(index>(Imagess.length-1))

                index=0;


        }

        selectImage();

    }


    private void selectImage() {

        mImage.setImageResource(Imagess[index]);

    }


}