Question

In: Computer Science

NEED TO REWRITE THIS IN OOP MODE ######## HOMEWORK 19 ###### Rewrite the code in the...

NEED TO REWRITE THIS IN OOP MODE

######## HOMEWORK 19
###### Rewrite the code in the OOP mode (class mode).


########################### lambda trick

##### first: changing label properties: after you've created a label, you
##### may want to change something about it. To do that, use configure method:

## label.configure(text = 'Yes')
## label.configure(bg = 'white', fg = 'black')

### that change the properties of a label called label.
###################################################
##
from tkinter import *
abc = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

def callback(x):
label.configure(text = 'Button {} clicked'. format(abc[x]))

root = Tk()

label = Label()
label.grid(row = 1, column = 0, columnspan = 26)

buttons = [0]*26 ##create a list to hold 26 buttons
for i in range(26):
buttons[i] = Button(text = abc[i],
command = lambda x = i: callback(x))
buttons[i].grid(row = 0, column = i)

mainloop()

Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

Note: Please maintain proper code spacing (indentation), just copy the code part and paste it in your compiler/IDE directly, no modifications required.

#code

from tkinter import *

''' class to represent the required GUI '''
class GUI:
    #constructor
    def __init__(self):
        #declaring all instance variables
        self.__root=Tk()
        self.__label=Label()
        #adding created label to the root window
        self.__label.grid(row=1, column=0, columnspan=26)
        self.__buttons=[]
        #setting up buttons
        self.__setup_buttons()

    #method to set up the buttons
    def __setup_buttons(self):
        #creating a list to store all alphabets
        letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
        #looping through each index
        for i in range(len(letters)):
            #creating button with text=current letter, adding command to call self.__callback method
            btn=Button(self.__root,text=letters[i],command = lambda x=i: self.__callback(x))
            #adding button to GUI
            btn.grid(row = 0, column = i)
            #adding button to list
            self.__buttons.append(btn)

    #event handler method
    def __callback(self,x):
        #getting text from button at x index and displaying on the label
        self.__label.configure(text='Button {} clicked'.format(self.__buttons[x]['text']))

    #method to make the window visible
    def set_visible(self):
        self.__root.mainloop()


#main module
if __name__ == '__main__':
    # creating a GUI and making window visible
    gui = GUI()
    gui.set_visible()

#output


Related Solutions

(This is for java) I need to rewrite this code that uses a while loop. public...
(This is for java) I need to rewrite this code that uses a while loop. public class Practice6 {      public static void main (String [] args) {         int sum = 2, i=2;        do { sum *= 6;    i++;    } while (i < 20); System.out.println("Total is: " + sum); }
1. You will need your ticker code (show your ticker code on your homework and include...
1. You will need your ticker code (show your ticker code on your homework and include a printout of the data!) for stock prices for this question. Use your ticker code to obtain the closing prices for the following three time periods to obtain three data sets: March 2, 2019 to March 16, 2019 Data set A March 17, 2019 to March 31, 2019 Data set B April 1, 2019 to April 17, 2019 Data set C To ensure equal...
Java homework problem: I need the code to be able to have a message if I...
Java homework problem: I need the code to be able to have a message if I type in a letter instead of a number. For example, " Please input only numbers". Then, I should be able to go back and type a number. import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class LoginGui {    static JFrame frame = new JFrame("JFrame Example");    public static void main(String s[]) {        JPanel panel...
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is...
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is my first java homework so for you i don't think it will be hard for you. (basic stuff) the problem: Write a complete Java program The transport Company in which you are the engineer responsible of operations for the optimization of the autonomous transport of liquid bulk goods, got a design contract for an automated intelligent transport management system that are autonomous trucks which...
I need solution of this homework Question: Queues, Deques and Priority Queues. Enter the necessary code
I need solution of this homework Question: Queues, Deques and Priority Queues. Enter the necessary code
Programming Java Homework: Find the Highest Score (Just need the code and directions followed exactly and...
Programming Java Homework: Find the Highest Score (Just need the code and directions followed exactly and written in Java code) Write a program that prompts the user to enter the number of students and each student’s name and score, and finally displays the name and score of the student with the highest score. Use the next () method in the Scanner class to read a name, rather than using the nextLine () method.
I need this in Java please: Behaviors. In the context of OOP, functions are called methods...
I need this in Java please: Behaviors. In the context of OOP, functions are called methods or behaviors because they typically do something. Most often, they read or change the values of one or more variables in the class. For example, you may have a weight variable in a class, and a method called gainWeight( ) that increases the weight variable by a certain amount. For this part of the lab, create class KoalaBear that has a weight attribute (in...
Rewrite this code of a game of Moropinzee so that it works as intended without the...
Rewrite this code of a game of Moropinzee so that it works as intended without the "break;" in the last few lines of the code. Code: import java.util.*; public class Moropinzee { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(true) { System.out.println("Player 1 enter a number 1-5 for Monkey, Robot, Pirate, Ninja, or Zombie:"); int p1 = sc.nextInt(); while(p1<1 || p1>5) { System.out.println("Invalid choice, Player 1. Enter a number 1-5:"); p1 = sc.nextInt(); } System.out.println("Player 2...
Hi, I'm trying to rewrite the code below (code #1) by changing delay() to millis(). void...
Hi, I'm trying to rewrite the code below (code #1) by changing delay() to millis(). void loop() { // Print the value inside of myBPM. Serial.begin(9600); int myBPM = pulseSensor.getBeatsPerMinute(); // Calls function on our pulseSensor object that returns BPM as an "int". // "myBPM" hold this BPM value now. if (pulseSensor.sawStartOfBeat()) { // Constantly test to see if "a beat happened". Serial.println("♥ A HeartBeat Happened ! "); // If test is "true", print a message "a heartbeat happened". Serial.print("BPM:...
Which Feature of OOP illustrates code reusability? Abstraction, Encapsulation, Inheritance, Polymorphism or All of them.
Which Feature of OOP illustrates code reusability? Abstraction, Encapsulation, Inheritance, Polymorphism or All of them.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT