| | |
Help with rewriting a program
![]() |
•
•
Join Date: Feb 2007
Posts: 19
Reputation:
Solved Threads: 0
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);
}
}
}
}
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);
}
}
}
}
•
•
Join Date: Jul 2006
Posts: 608
Reputation:
Solved Threads: 152
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
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
![]() |
Similar Threads
- Playing .Wav/MIDI files in a Visual Basic Program (Visual Basic 4 / 5 / 6)
- What's the HARDEST program you've written? (Computer Science)
- Cool little Program to disable startup programs (Windows NT / 2000 / XP)
- array help plz! (C++)
- Program is shutting down right after program is executed (C++)
- 3d Program (Game Development)
Other Threads in the Python Forum
- Previous Thread: adding numbers in tk
- Next Thread: How to use wxPython demo
Views: 1103 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for Python
application array beginner c++ c/c++ change character class client code command compression convert count create csv ctypes database dictionary django dll error examples excel exe extensions fdlib file float format framework ftp function graphics gui homework image images import input library line linux list lists logging loop loops microcontroller mouse mysql mysqldb number numbers output parse parsing path port prime processing program programming py2exe pygame pygtk pyqt pysimplewizard python random raw_input recursion recursive redirect remote scrolledtext server socket ssh stdout string strings syntax table terminal text thread threading tkinter transparency tuple tutorial ubuntu unicode unix variable variables web windows wxpython





