Question

########### Homework 20 ################################## ################################################### #### Rewrite this code in 'class mode' : define the class...

########### Homework 20
##################################

###################################################
#### Rewrite this code in 'class mode' : define the class and so on....
#### If you can - try make a clicked button 'blue'.
#### (Only if you can.)

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

In case of any query, do comment. Please rate the answer.

Note: While copying the code from Chegg Windows, adds some extra characters and it disturbs the indentation as well. Please be cautious while copying. Please refer the screen shot of the code for indentation of the code.

Code:

from tkinter import *

root = Tk()

class click_buttons:

    #constructor of click_buttons

    def __init__(self):

        #atatch labels, buttons to class using self

        self.label = Label()

        self.abc = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

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

        self.buttons = [0]*26 ##create a list to hold 26 buttons

        for i in range(26):

            #assign callback functions

            self.buttons[i] = Button(text = self.abc[i],

            command = lambda x = i: self.callback(x))

            self.buttons[i].grid(row = 0, column = i)

    #callback method for button click

    def callback(self,x):

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

        self.buttons[x].configure(bg="blue")

#create a object of the click_buttons class

click = click_buttons()

mainloop()

=========screen shot of the code for indentation=============

Output:

Add a comment
Know the answer?
Add Answer to:
########### Homework 20 ################################## ################################################### #### Rewrite this code in 'class mode' : define the class...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Help with Python code. Right now I'm creating a code in Python to create a GUI....

    Help with Python code. Right now I'm creating a code in Python to create a GUI. Here's my code: #All modules are built into Python so there is no need for any installation import tkinter from tkinter import Label from tkinter import Entry def calculatewages(): hours=float(nhours.get()) nsal=float(nwage.get()) wage=nsal*hours labelresult=Label(myGUI,text="Weekly Pay: $ %.2f" % wage).grid(row=7,column=2) return Tk = tkinter.Tk()    myGUI=Tk myGUI.geometry('400x200+100+200') myGUI.title('Pay Calculator') nwage=float() nhours=float() label1=Label(myGUI,text='Enter the number of hours worked for the week').grid(row=1, column=0) label2=Label(myGUI,text='Enter the pay rate').grid(row=2, column=0)...

  • Demonstrate this result for the parallel calculation. CODE: from Tkinter import * # MODIFY THIS TO...

    Demonstrate this result for the parallel calculation. CODE: from Tkinter import * # MODIFY THIS TO HANDLE THREE RESISTORS __author__ = "robincarr" """ GUI version of Resistor Calculator finds the equivalent resistance for two resistors connected either in series or parallel. Features entry widgets and buttons.""" def series_calculation(): r1 = float(resistor1.get()) r2 = float(resistor2.get()) req = r1 + r2 equivalent_resistance.delete(0, END) # Clear the previous result. equivalent_resistance.insert(0, req) print("Resistor 1: %s\nResistor 2: %s" % (r1, r2)) print "The equivalent series...

  • Python 3 Code does not save the data into excel properly ​​​​​​​ # required library import...

    Python 3 Code does not save the data into excel properly ​​​​​​​ # required library import tkinter as tk from tkcalendar import DateEntry import xlsxwriter # frame window = tk.Tk() window.title("daily logs") #window.resizable(0,0) # labels tk.Label(window, text="Bar code").grid(row=0, sticky="W", pady=20, padx=20) tk.Label(window, text="Products failed").grid(row=1, sticky="W", pady=20, padx=20) tk.Label(window, text="Money Lost").grid(row=2, sticky="W", pady=20, padx=20) tk.Label(window, text="Failed date").grid(row=3, sticky="W", pady=20, padx=20) # entries barcode = tk.Entry(window) product = tk.Entry(window) money = tk.Entry(window) # arraging barcode.grid(row=0, column=1) product.grid(row=1, column=1) money.grid(row=2, column=1) cal =...

  • Python 3 Fix the code so if the user enter the same bar code more than...

    Python 3 Fix the code so if the user enter the same bar code more than three times, it shows a warning message indicating that the product was already tested 3 times and it reached the limits Code: import tkinter as tk from tkcalendar import DateEntry from openpyxl import load_workbook from tkinter import messagebox from datetime import datetime window = tk.Tk() window.title("daily logs") window.grid_columnconfigure(1,weight=1) window.grid_rowconfigure(1,weight=1) # labels tk.Label(window, text="Bar code").grid(row=0, sticky="W", pady=20, padx=20) tk.Label(window, text="Products failed").grid(row=1, sticky="W", pady=20, padx=20) tk.Label(window,...

  • " In the workshop exercises you have used Python's Tkinter module to create simple Graphical User Interfaces. The following code is an incomplete Python program relying on Tkinter (with delib...

    " In the workshop exercises you have used Python's Tkinter module to create simple Graphical User Interfaces. The following code is an incomplete Python program relying on Tkinter (with deliberately unhelpful variable and function names) which you must complete by filling in the blanks. When complete the program should create a window with two buttons, labelled and!!', respectively. When the button la belled'???' is pushed nothing happens. When the button labelled'!!"is pushed both buttons' labels are set to'!!!' from tkinter...

  • Need help with Intro to Comp Sci 2 (Python) problem: This is what is provided: (Copy/Paste...

    Need help with Intro to Comp Sci 2 (Python) problem: This is what is provided: (Copy/Paste version): from tkinter import Tk, Label, Entry, Button from random import * class Craps(Tk): #set up the main window def __init__(self, parent=None): Tk.__init__(self, parent) self.title('Play Craps') self.new_game() self.make_widgets() #when a new game is started, the firstRoll will start at 0 def new_game(self): self.firstRoll = 0    #create and place the widgets in the window def make_widgets(self): Label(self, text="Die 1").grid(row=0, column=0, columnspan=1) Label(self, text="Die 2").grid(row=0,...

  • The Gui has all the right buttons, but from there i get lost. I need to...

    The Gui has all the right buttons, but from there i get lost. I need to know whats wrong with my assignment can someone please help. The code I have so far is listed below, could you please show me the errors in my code. PYTHON Create the GUI(Graphical User Interface). Use tkinter to produce a form that looks much like the following. It should have these widgets. Temperature Converter GUI Enter a temperature (Entry box)                 Convert to Fahrenheit...

  • Python 3 How to change the icon in the error message want to put a yellow...

    Python 3 How to change the icon in the error message want to put a yellow warning icon import tkinter as tk from tkcalendar import DateEntry from openpyxl import load_workbook from tkinter import messagebox window = tk.Tk() window.title("daily logs") window.grid_columnconfigure(1,weight=1) window.grid_rowconfigure(1,weight=1) # labels tk.Label(window, text="Bar code").grid(row=0, sticky="W", pady=20, padx=20) tk.Label(window, text="Products failed").grid(row=1, sticky="W", pady=20, padx=20) tk.Label(window, text="Money Lost").grid(row=2, sticky="W", pady=20, padx=20) tk.Label(window, text="sold by").grid(row=3, sticky="W", pady=20, padx=20) tk.Label(window, text="Failed date").grid(row=4, sticky="W", pady=20, padx=20) # entries barcode = tk.Entry(window) product =...

  • Im posting a python program and need to convert it to C++ language. import Tkinter import...

    Im posting a python program and need to convert it to C++ language. import Tkinter import Tkinter as tk from Tkinter import * import time def current_iso8601():     """Get current date and time in ISO8601"""     # https://en.wikipedia.org/wiki/ISO_8601     # https://xkcd.com/1179/     return time.strftime("%m.%d.%Y DATE %H:%M:%S TIME") class Application(tk.Frame):     def __init__(self, master=None):                 tk.Frame.__init__(self, master)         self.pack()         self.createWidgets()         def tstampIn(self):                 clockFile = open("clockTracker.txt","w")         print "You have successfuly Clocked In \n"         clockIn...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT