在Python中使用类的Button命令

时间:2015-07-19 17:41:26

标签: python python-3.x button tkinter

我正在努力做到这一点,但到目前为止还没有运气。如果有人可以帮助我,我将不胜感激。

package com.pradhul.game.touchball;
import android.annotation.SuppressLint;
import android.graphics.Canvas;

public class GameLoopThread extends Thread {

private GameView view;
private boolean running = false;

public GameLoopThread(GameView view){
    this.view = view;
}
public void setRunning(boolean run){
    running = run;
}

@SuppressLint("WrongCall")
public void run(){
    while (running){
        Canvas canvas = null;
        try{
            canvas = view.getHolder().lockCanvas();
            synchronized (view.getHolder()){
                view.onDraw(canvas);
            }
        }finally{
            if(canvas != null) {
                view.getHolder().unlockCanvasAndPost(canvas);
            }
        }
    }

}
}

单击package com.pradhul.game.touchball; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.util.Log; import java.util.Random; public class Balloon { private static final int BALLOON_SPEED = 10; private int y = 0; private int x = 0; private int speed = 1; private GameView gameView; private Bitmap balloon; public Bitmap[] normalBalloons; private int balloonIndex = 0; private int normalImages[] = {R.drawable.normal_01,R.drawable.normal_02,R.drawable.normal_03, R.drawable.normal_04,R.drawable.normal_05,R.drawable.normal_06,R.drawable.normal_07, R.drawable.normal_08, }; private int crackingImages[] = {R.drawable.crack_01,R.drawable.crack_02,R.drawable.crack_03, R.drawable.crack_04, R.drawable.crack_05,R.drawable.crack_04,R.drawable.crack_03, R.drawable.crack_02 }; private boolean reverseSwap = false; public Balloon(GameView gameView){ this.gameView = gameView; normalBalloons = new Bitmap[8]; setUpImages(); } public void onDraw(Canvas canvas){ /*draws the balloon in canvas */ animateBalloon(); update(canvas.getWidth()); canvas.drawBitmap(balloon, x, y, null); } public boolean isCollision(float x2, float y2) { return x2 > x && x2 < x + balloon.getWidth() && y2 > y && y2 < y + balloon.getHeight(); } private int getRandomX(int maxVal) { Random rand = new Random(); return rand.nextInt(maxVal); } private void animateBalloon() { /*Animates the balloon by swapping resource image at each call*/ this.balloon = getBalloons(); Log.d("Balloon",balloonIndex % normalBalloons.length + ""); } private void update(int canvasWidth) { /*updates the y position for moving the balloon*/ if (y <= 0){ /*so that the balloon starts from bottom * gameView will return a height only after the View is ready * getting 0 in constructor of this class*/ y = gameView.getHeight(); /*x is assigned a random between the width od the canvas * so that the balloons will appear random positions from below*/ x = getRandomX(canvasWidth - balloon.getWidth()); } if (y > gameView.getHeight() - balloon.getHeight() - speed) { speed = -BALLOON_SPEED; } y = y + speed; Log.d("Balloon","Positions:"+x+","+y); } private Bitmap getBalloons() { if(balloonIndex == normalBalloons.length-1) { reverseSwap = true; } if(balloonIndex == 0){ reverseSwap = false; } balloonIndex = reverseSwap?balloonIndex-1:balloonIndex+1; return normalBalloons[balloonIndex]; } private void setUpImages() { /*setting up resources array*/ for(int count =0; count < normalImages.length; count++){ Bitmap balloon = BitmapFactory.decodeResource(gameView.getResources(), normalImages[count]); normalBalloons[count] = balloon; } } } 按钮但import tkinter class MyGUI: def __init__(self): self.main_window = tkinter.Tk() self.button1 = tkinter.Button(self.main_window,text='Average',command=self.average) self.button1.pack() tkinter.mainloop() def average(self): self.mini_window = tkinter.Tk() self.avg_mess = tkinter.Label(self.mini_window,text='Results:') self.avg_result_var = tkinter.StringVar() self.avg_result_display = tkinter.Label(self.mini_window,textvariable=self.avg_result_var) self.avg_mess.pack() self.avg_result_display.pack() self.button2 = tkinter.Button(self.mini_window,text='Calculate',command=self.avg_calc) self.button2.pack() def avg_calc(self): self.avg_result = (100+300+80)/3 self.avg_result_var.set(self.avg_result) gui = MyGUI() 未更改其值时,会出现此问题。因此Calculate仍然是空白的。我怀疑按下按钮时函数调用有问题。我正在使用Python 3.x.感谢。

1 个答案:

答案 0 :(得分:0)

你几乎正确地做了,但有几个问题

首先,结果永远不会改变,因为每次进行计算时都使用相同的数字。结果总是一样的,所以它似乎没有改变。

第二个问题是您正在创建Tk的两个实例。 Tkinter的设计并不像那样,它会导致诸如您正在观察的问题。如果您需要其他弹出窗口,请使用Toplevel而不是Tk

这是你的程序的修改版本,虽然我在计算中添加了一个随机数,所以你可以看到它每次都改变。

import Tkinter as tkinter
import random
class MyGUI:
    def __init__(self):
        self.main_window = tkinter.Tk()
        self.button1 = tkinter.Button(self.main_window,text='Average',command=self.average)
        self.button1.pack()
        tkinter.mainloop()
    def average(self):
        self.mini_window = tkinter.Toplevel()
        self.avg_mess = tkinter.Label(self.mini_window,text='Results:')
        self.avg_result_var = tkinter.StringVar()
        self.avg_result_display = tkinter.Label(self.mini_window,textvariable=self.avg_result_var)
        self.avg_mess.pack(fill="both")
        self.avg_result_display.pack()
        self.button2 = tkinter.Button(self.mini_window,text='Calculate',command=self.avg_calc)
        self.button2.pack()
    def avg_calc(self):
        x = random.randint(100,200)
        self.avg_result = (100+300+x)/3
        print "result:", self.avg_result
        self.avg_result_var.set(self.avg_result)
gui = MyGUI()