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); }
Need someone to rewrite this code to work in the same way: int testScore;    //Numeric test...
Need someone to rewrite this code to work in the same way: int testScore;    //Numeric test score         String input;     //To hold the user's input                 // Get the numeric test score.         input = JOptionPane.showInputDialog("Enter your numeric test score and I will tell you the grade: ");         testScore = Integer.parseInt(input);                 //Display the grade.         if (testScore < 60)            JOptionPane.showMessageDialog(null, "Your score of "+testScore+" is an F.");         else if (testScore < 70)            JOptionPane.showMessageDialog(null,...
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.
USING C++ ONLY. Please study the code posted below. the goal is to rewrite the code...
USING C++ ONLY. Please study the code posted below. the goal is to rewrite the code implementing a template class using a linked list instead of an array. Note: The functionality should remain the same. /** * Queue implementation using linked list C style implementation ( no OOP). */ #include <cstdio> #include <cstdlib> #include <climits> #include <iostream> #define CAPACITY 100 // Queue max capacity using namespace std; /** Queue structure definition */ struct QueueType { int data; struct QueueType *...
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...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT