Help me with phthon GUI. I have a template provided by professor. I need to comp
ID: 3930586 • Letter: H
Question
Help me with phthon GUI.
I have a template provided by professor. I need to complete it.
I will give you a template, so copy and paste into your python file:
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
from tkinter import *
from tkinter.font import *
from tkinter.ttk import *
from tkinter import *
# the template code generates the top frame on the GUI
# and the bottom frame. The top frame contains the Entry
# object and the "Send" key. If the Entry key is double-clicked,
# the Entry object is cleared.
# I've also set up a "letters" variable to reflect the relative positions
# of letters on a keyboard. The first row ['q','w','e',...] is letters[0].
# Note that since letters is a 2-dimensional list, letters[0] is also a list.
# This organization may help you set up frames, one for each row on the keyboard.
class Phone:
def __init__(self):
window = Tk()
# self.buttons is a dictionary; Key in this dictionary are
# Button objects. An entry's value is the label on that button.
self.buttons = dict()
# Here are the letters as they will be laid out on the
# keyboard. Notice this is a 2-dimensional list. The
# rows in the list correspond to rows of keys (Buttons)
# on the keyboard. For example, 'q' is the leftmost
# letter on the first row (below the Entry object) and
# 'p' is the rightmost; 'a' is the leftmost letter on the
# next row, etc. '<' is meant to be the backspace key.
letters = [['q','w','e','r','t','y','u','i','o','p'],
['a','s','d','f','g','h','j','k','l'],
['z','x','c','v','b','n','m','<']]
# a StringVar is a mutable string
# "msg" is a StringVar that is displayed on the Entry called "e"
self.msg = StringVar()
# This frame holds e, plus the "Send" key
f = Frame(window)
f.pack()
self.e = Entry(f,textvar=self.msg) #,justify='right')
# columnspan=6 makes the Entry wide
self.e.grid(row=0,column=0,columnspan=6,sticky=N+S+E+W)
# The "Send" key is next to the Entry
b = Button(f,text='Send',font=Font(family='Arial',size=6))
b.grid(row=0,column=6)
#############################################
# Set up the event handler for the send key
# Just requires one line of code
#############################################
###################################################
# Write the code that places 26 Buttons
# on the GUI, one for each letter, with the
# layout corresponding to the "letters" variable
# above. To do this, place create a separate Frame
# for each row of Buttons on the keyboard, and then
# pack each Frame into the Tk window.
###################################################
###################################################
# Below, I've provided the code to set up the space
# key and the '.' key next to it
# The space key has no label on it.
###################################################
f = Frame(window)
space = Button(f,width=10)
# columnspan is used to make the space key larger
space.grid(row=0,column=0,columnspan=3)
self.buttons[space] = ' '
# Here is the '.' key
dot = Button(f,text='.')
self.buttons[dot] = '.'
dot.grid(row=0,column=3)
###################################################
# Set up event-handlers for the space and dot keys.
# You can probably use the same event-handler
# as for the letter keys.
###################################################
f.pack()
######################################################
# Below, write the event-handling code for the various
# buttons.
######################################################
def type(self, event):
pass
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Explanation / Answer
python 2.7.12 version
import sys
import Tkinter
from Tkinter import *
Keyboard_app=Tkinter.Tk()
Keyboard_app.title("Keyboard")
Keyboard_app.resizable(0,0)
def select(value):
if value == "<-":
entry2 = entry.get()
pos = entry2.find(' ')
pos2 = entry2[pos:]
entry.delete(pos2,Tkinter.END)
elif value == "Space":
entry.insert(Tkinter.END,' ')
elif value == "Tab":
entry.insert(Tkinter.END,' ')
else:
entry.insert(Tkinter.END,value)
buttons = [
'q','w','e','r','t','y','u','i','o','p',
'a','s','d','f','g','h','j','k','l',
'z','x','c','v','b','n','m','Space','Tab','<-','.'
]
label1 = Label(Keyboard_app,text='').grid(row=0,column = 1)
entry = Entry(Keyboard_app, width = 80)
entry.grid(row = 1, columnspan = 15)
varRow = 2
varColumn = 0
for button in buttons:
command = lambda x=button:select(x)
if button != "Space":
Tkinter.Button(Keyboard_app,text = button,width = 5,bg="#0000ff",fg="#ffffff",
activebackground = "#ffffff", activeforeground="#000000",relief='raised',padx=4,
pady=4,bd=4,command=command).grid(row = varRow,column = varColumn)
if button == "Space":
Tkinter.Button(Keyboard_app,text = button,width = 60,bg="#0000ff",fg="#ffffff",
activebackground = "#ffffff", activeforeground="#000000",relief='raised',padx=4,
pady=4,bd=4,command=command).grid(row = 6,columnspan = 16)
varColumn = varColumn + 1
if varColumn > 14 and varRow == 2:
varColumn = 0
varRow = varRow + 1
if varColumn > 14 and varRow==3:
carColumn = 0
varRow = varRow + 1
Keyboard_app.mainloop()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.