943,844 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 1190
  • Python RSS
Apr 15th, 2007
0

Help with rewriting a program

Expand Post »
I wrote a program in java that takes a message entered in the shell and flashes the message, then click the quit button to quit.

Now I am supposed to rewrite it in python. So far in TKinter I only have the actual quit button (gui).

I have no idea how to translate the thread part from java to python.

Thanks in advance.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FlashingText extends JFrame implements ActionListener{

public static void main(String []args){
for (int i = 0; i < args.length; i++)
System.out.println("Args: " + args [i]);

//Create the frame and set a title
JFrame frame = new JFrame ("Flashing Text");

JPanel textPanel = new JPanel();
textPanel.setBackground(Color.black);
Lights lights = new Lights (args);
textPanel.add(lights);

//Set the frame layout
frame.setLayout(new FlowLayout());

//Create a quit button and add an action listener to the button

JPanel quitPanel = new JPanel();
JButton quitbutton = new JButton("QUIT");
quitPanel.add(quitbutton);
quitbutton.addActionListener( new FlashingText () );

// Add the components to the frame
Container content = frame.getContentPane();
content.add(quitPanel);
content.add(textPanel);


//Set the size of the frame and set to visible
frame.setSize(100, 50);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public void actionPerformed(ActionEvent ae){
System.exit(0);
}
}


Second class:

import java.awt.*;
import javax.swing.*;

public class Lights extends JPanel implements Runnable {

Font textFont = new Font("Serif", Font.BOLD, 20);
Thread runner;
JLabel [] input;

public Lights(String [] message) {

input = new JLabel [message.length];
for (int i = 0; i < message.length; i++){
input [i]= new JLabel(message[i]);
this.add(input[i]);
}

runner = new Thread(this);
runner.start();
}

void pause(int duration) {
try {
Thread.sleep(duration);
} catch (InterruptedException e) { }
}

public void run() {
while ( true ) {
for (int i = 0; i < input.length; i++){
input[i].setForeground(new Color (180, 150, 195));
pause(70);
pause(70);
}

for (int i = 0; i < input.length; i++){
input[i].setForeground (Color.black);
pause(70);
pause(70);
}
}
}
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
alba07 is offline Offline
19 posts
since Feb 2007
Apr 15th, 2007
0

Re: Help with rewriting a program

You'll want to take a look at the Entry widget in the manual:

http://infohost.nmt.edu/tcc/help/pubs/tkinter/

The good news is that no threading is needed. You can simply bind events like <Enter> to the Entry widget and have it perform the desired actions.

Jeff
Reputation Points: 92
Solved Threads: 156
Practically a Master Poster
jrcagle is offline Offline
608 posts
since Jul 2006
Apr 15th, 2007
0

Re: Help with rewriting a program

thank you for this helpful info.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
alba07 is offline Offline
19 posts
since Feb 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: adding numbers in tk
Next Thread in Python Forum Timeline: How to use wxPython demo





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC