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 );

//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 = new JLabel(message);
this.add(input);
}

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.setForeground(new Color (180, 150, 195));
pause(70);
pause(70);
}

for (int i = 0; i < input.length; i++){
input.setForeground (Color.black);
pause(70);
pause(70);
}
}
}
}

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

thank you for this helpful info.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.