I've written a program that basically plots different data upon pressing different buttons. The program works as intended under Windows, but when I tried to port it to Linux (Red Hat v6) I'm getting a strange issue: the window that I want to plot does not appear until after I close the main program. This happens regardless of the figure (figure 1,2 etc.) I'm trying to plot to, or if I try to type plt.show() etc.
The program I've written is almost 1000 lines of code, but I created an abbreviated program that has the same problem. It works under Windows, but under Linux I have to close the root window for the matplotlib window to appear.
Working code:
import matplotlib.pyplot as plt
from tkinter import *
def click():
x=['0','1','2']
plt.plot(x,x)
plotGUI=Tk()
butt1=Button(plotGUI,text="Test", command=click).grid()
plotGUI.mainloop()
If the reduced code still does not show up a Tk-toplevel window, add a line of:
plotGUI.lift() # force WM to raise Tk() window
plotGUI.mainloop()
If the reduced code has problems with a matplotlib-wrapper, would be necessary to be more specific on what wrapping method you use to get matplitlib-output into a Tkinter Canvas et al.
If the code attempts to rely on default matplotlib tools alike plt.show() then the code will suffer from having two adjacent .mainloop()-s -- first the Tk()'s -- second one hidden in default matplotlib-s .show() -- and thus your code would become hardly controllable of the two adjacent UI-s.
co-integrated UI
For having non-adjacent UI-controllers and more benefits of the co-integrated UI,
try to re-use backends factories to plot directly on Tkinter.Canvas and other plausible widgets of your choice and control.
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
and proceed to a fully integrated GUI-MVC layers on both your code & matplotlib Model-state / inputs-Controller / Visual-output.
a bit of more code for co-integrated UI sample:
class SuperShapeFrame( Frame ): # The user interface:
def __init__( self, master = None ):
Frame.__init__( self, master )
self.grid()
self.m = 3
self.n1 = 2
self.n1_scaling = LinearScaling( ( .1, 20 ), ( 0, 200 ) )
self.n2 = 18
self.n2_scaling = LinearScaling( ( .1, 20 ), ( 0, 200 ) )
self.n3 = 18
self.n3_scaling = LinearScaling( ( .1, 20 ), ( 0, 200 ) )
self.fig = Figure( ( 6, 6 ), dpi = 100 )
canvas = FigureCanvasTkAgg( self.fig, master = self )
canvas.get_tk_widget().grid( row = 0, column = 0, columnspan = 4 )
label = Label( self, text = 'M' )
label.grid( row = 1, column = 1 )
self.m_slider = Scale( self, from_ = 1, to = 20, \
orient = HORIZONTAL, command = lambda i : self.update_m() \
)
self.m_slider.grid( row = 1, column = 2 )
label = Label( self, text = 'N1' )
label.grid( row = 2, column = 1 )
self.n1_slider = Scale( self, from_ = 0, to = 200, \
orient = HORIZONTAL, command = lambda i : self.update_n1() \
)
self.n1_slider.grid( row = 2, column = 2 )
label = Label( self, text = 'N2' )
label.grid( row = 3, column = 1 )
self.n2_slider = Scale( self, from_ = 0, to = 200, \
orient = HORIZONTAL, command = lambda i : self.update_n2() \
)
self.n2_slider.grid( row = 3, column = 2 )
label = Label( self, text = 'N3' )
label.grid( row = 4, column = 1 )
self.n3_slider = Scale( self, from_ = 0, to = 200,
orient = HORIZONTAL, command = lambda i : self.update_n3() \
)
self.n3_slider.grid( row = 4, column = 2 )
self.draw_figure() # >>> ================================================================ DRAW FIRST APPEARANCE OF THE INSTANCE
def update_m( self ):
self.m = self.m_slider.get()
self.refresh_figure() # >>> .-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. UPDATE
def update_n1( self ):
self.n1 = self.n1_scaling.dst_to_src( self.n1_slider.get() )
self.refresh_figure() # >>> .-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. UPDATE
def update_n2( self ):
self.n2 = self.n2_scaling.dst_to_src( self.n2_slider.get() )
self.refresh_figure() # >>> .-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. UPDATE
def update_n3(self):
self.n3 = self.n3_scaling.dst_to_src( self.n3_slider.get() )
self.refresh_figure() # >>> .-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. UPDATE
def refresh_figure( self ): # <<< .-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. UPDATE ACTUAL APPEARANCE OF THE INSTANCE
r = supershape_radius( self.phi, 1, 1, self.m, self.n1, self.n2, self.n3 ) # .CALC new polar values in radius dimension
self.lines.set_ydata( r ) # .MOD <lines>, selectively just their <lines>.set_ydata() coordinates
self.fig.canvas.draw_idle() # .GUI MVC-Visual part UPDATE via <self>.<fig>.<canvas>.draw_idle()
def draw_figure( self ): # <<< =============================================================== DRAW FIRST APPEARANCE OF THE INSTANCE
self.phi = np.linspace( 0, 2 * np.pi, 1024 ) # .STO <phi> a np.array with static fi-coordinates
r = supershape_radius( self.phi, 1, 1, self.m, self.n1, self.n2, self.n3 )
ax = self.fig.add_subplot( 111, polar = True ) #
self.lines, = ax.plot( self.phi, r, lw = 3. ) # .STO <lines> aListOfLINEs from .plot() function
self.fig.canvas.draw() # .GUI MVC-Visual part, enforce first visual output via <self>.<fig>.<canvas>.draw()
def TkDemo(): # Finally, we set up and start our user interface:
""" HELP: CookBook: Tk-GUI-MVC via SuperShape example
TESTS: TkDemo()
"""
root = Tk()
root.lift()
root.protocol( 'WM_DELETE_WINDOW', root.quit() ) # [X]-overide ---------------------------
app = SuperShapeFrame( root ) # <<<--- pass <root>
app.master.title( 'CookBook: Tk-GUI-MVC via SuperShape' )
app.mainloop()
pass
Full-code for [Halldinz0r] copy/paste re-testing as-is:
####################################################################### #
###
### TkDemo()
###
### KCA_plot_inTk ##################################################### # Tk() GUI ###################################################################################################### _plot_inTk TkDemo() #################
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
def supershape_radius( phi, a, b, m, n1, n2, n3 ): # define the function for the SuperShape curve using the following code:
theta = .25 * m * phi
cos = np.fabs( np.cos( theta ) / a ) ** n2
sin = np.fabs( np.sin( theta ) / b ) ** n3
r = ( cos + sin ) ** ( -1. / n1 )
r /= np.max( r )
return r
class LinearScaling( object ): # define a utility object to linearly scale a range into another as follows:
def __init__( self, src_range, dst_range ):
self.src_start, src_diff = src_range[0], src_range[1] - src_range[0]
self.dst_start, dst_diff = dst_range[0], dst_range[1] - dst_range[0]
self.src_to_dst_coeff = dst_diff / src_diff
self.dst_to_src_coeff = src_diff / dst_diff
def src_to_dst( self, X ):
return ( X - self.src_start ) * self.src_to_dst_coeff + self.dst_start
def dst_to_src( self, X ):
return ( X - self.dst_start) * self.dst_to_src_coeff + self.src_start
class SuperShapeFrame( Frame ): # The user interface:
def __init__( self, master = None ):
Frame.__init__( self, master )
self.grid()
self.m = 3
self.n1 = 2
self.n1_scaling = LinearScaling( ( .1, 20 ), ( 0, 200 ) )
self.n2 = 18
self.n2_scaling = LinearScaling( ( .1, 20 ), ( 0, 200 ) )
self.n3 = 18
self.n3_scaling = LinearScaling( ( .1, 20 ), ( 0, 200 ) )
self.fig = Figure( ( 6, 6 ), dpi = 100 )
canvas = FigureCanvasTkAgg( self.fig, master = self )
canvas.get_tk_widget().grid( row = 0, column = 0, columnspan = 4 )
label = Label( self, text = 'M' )
label.grid( row = 1, column = 1 )
self.m_slider = Scale( self, from_ = 1, to = 20, \
orient = HORIZONTAL, command = lambda i : self.update_m() \
)
self.m_slider.grid( row = 1, column = 2 )
label = Label( self, text = 'N1' )
label.grid( row = 2, column = 1 )
self.n1_slider = Scale( self, from_ = 0, to = 200, \
orient = HORIZONTAL, command = lambda i : self.update_n1() \
)
self.n1_slider.grid( row = 2, column = 2 )
label = Label( self, text = 'N2' )
label.grid( row = 3, column = 1 )
self.n2_slider = Scale( self, from_ = 0, to = 200, \
orient = HORIZONTAL, command = lambda i : self.update_n2() \
)
self.n2_slider.grid( row = 3, column = 2 )
label = Label( self, text = 'N3' )
label.grid( row = 4, column = 1 )
self.n3_slider = Scale( self, from_ = 0, to = 200,
orient = HORIZONTAL, command = lambda i : self.update_n3() \
)
self.n3_slider.grid( row = 4, column = 2 )
self.draw_figure() # >>> ================================================================ DRAW FIRST APPEARANCE OF THE INSTANCE
def update_m( self ):
self.m = self.m_slider.get()
self.refresh_figure() # >>> .-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. UPDATE
def update_n1( self ):
self.n1 = self.n1_scaling.dst_to_src( self.n1_slider.get() )
self.refresh_figure() # >>> .-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. UPDATE
def update_n2( self ):
self.n2 = self.n2_scaling.dst_to_src( self.n2_slider.get() )
self.refresh_figure() # >>> .-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. UPDATE
def update_n3(self):
self.n3 = self.n3_scaling.dst_to_src( self.n3_slider.get() )
self.refresh_figure() # >>> .-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. UPDATE
def refresh_figure( self ): # <<< .-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. UPDATE ACTUAL APPEARANCE OF THE INSTANCE
r = supershape_radius( self.phi, 1, 1, self.m, self.n1, self.n2, self.n3 ) # .CALC new polar values in radius dimension
self.lines.set_ydata( r ) # .MOD <lines>, selectively just their <lines>.set_ydata() coordinates
self.fig.canvas.draw_idle() # .GUI MVC-Visual part UPDATE via <self>.<fig>.<canvas>.draw_idle()
def draw_figure( self ): # <<< =============================================================== DRAW FIRST APPEARANCE OF THE INSTANCE
self.phi = np.linspace( 0, 2 * np.pi, 1024 ) # .STO <phi> a np.array with static fi-coordinates
r = supershape_radius( self.phi, 1, 1, self.m, self.n1, self.n2, self.n3 )
ax = self.fig.add_subplot( 111, polar = True ) #
self.lines, = ax.plot( self.phi, r, lw = 3. ) # .STO <lines> aListOfLINEs from .plot() function
self.fig.canvas.draw() # .GUI MVC-Visual part, enforce first visual output via <self>.<fig>.<canvas>.draw()
def TkDemo(): # Finally, set up and start our user interface:
""" HELP: CookBook: Tk-GUI-MVC via SuperShape example
TESTS: TkDemo()
"""
root = Tk()
root.lift()
root.protocol( 'WM_DELETE_WINDOW', root.quit() ) # [X]-overide ---------------------------
app = SuperShapeFrame( root ) # <<<--- pass <root>
app.master.title( 'CookBook: Tk-GUI-MVC via SuperShape' )
app.mainloop()
pass
### ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ _plot_inTk TkDemo() ^^^^^^^^^^^^^^^^^^
I was able to make it work simply by adding:
matplotlib.use('TkAgg')
This made the program work just as in Windows, without any other modifications necessary. I really need to learn the concepts posted by user3666197 for future projects though.
Related
I am creating a brightness toggle on Python. I am using the Tkinter module to create a sider function. I have successfully created a slider to increase/decrease the volume. However, I want to translate this into one where I can increase/decrease the screen's brightness. Here is my volume code and output.
I am currently using macOS Monterey Version 12.2.1.
#The GUI player
import pygame
from pygame import mixer
from tkinter import *
# Makes a new window and stores it inside the root variable.
root = Tk()
mixer.init() # initialising the mixer
# Coding the background music
def play_music():
pygame.mixer.music.load("ArcadeMusic copy.mp3")
pygame.mixer.music.play(-1)
def set_vol(val):
volume = int(val) / 100
mixer.music.set_volume(volume)
root.geometry('300x70')
root.title("Volume")
text = Label(root, text = "Drag the slider to adjust volume")
text.pack()
scale = Scale(root,from_=0,to=100 , orient=HORIZONTAL, command=set_vol)
scale.pack()
play_music()
root.mainloop()
Are you trying to achieve something like this? Or do you want to control desktop brightness?
import tkinter
def find( obj ):
name = f"{type(obj).__name__}:n "
try:
return name + "n ".join( [ f"{x} = '{obj.cget(x)}'" for x in obj.keys() ] )
except:
return f"'{obj}' has no keys attribute"
class brightness:
def __init__( self ):
self.master = tkinter.Tk()
self.master.update_idletasks()
self.color = 15790320 # #f0f0f0 = SystemButtonFace
self.var = tkinter.IntVar( self.master, value = self.color )
self.flexx( self.master )
self.label = tkinter.LabelFrame(
self.master, labelanchor = 'n', text = self.master['background'] )
self.label.grid( row=0, column=0, columnspan=2, sticky='nsew' )
self.flexx( self.label, r=None )
self.flexx( self.label, r=None , c=1 )
self.scroll = tkinter.Scale(
self.label, orient = 'horizontal',
resolution = 65793, label = 'Brightness Control',
from_ = 0, to = 16777215, variable = self.var,
command = self.control )
self.scroll.grid( row = 1, column=0, columnspan=2, sticky='ew' )
self.master.geometry( '200x200' )
self.master.minsize( 334, 113 )
def flexx( self, o, r = 0, c = 0, rw = 1, cw = 1 ):
if r != None:
o.rowconfigure( r, weight = rw )
if c != None:
o.columnconfigure( c, weight = cw )
def convert( self ):
col = '#' + ( '000000' + hex( self.color )[2:])[~5:]
self.var.set( self.color )
self.label['text'] = col
self.master.tk_setPalette( col )
def control_up( self ):
self.color += 65793
if self.color > 16777215:
self.color = 15790320
col = self.convert( )
def control_down( self ):
self.color -= 65793
if self.color < 0: # 15790320:
self.color = 16777215
col = self.convert( )
def control( self, n ):
self.color = int( n )
col = self.convert( )
if __name__ == '__main__':
bright = brightness( )
tkinter.mainloop()
I have this table in tkinter. However, I want to have only one button in each row with four text fields. (I currently have four buttons in each row.)
Basically, I want to connect these four buttons to one button in a row. How can I do this?
from tkinter import *
class Table:
def __init__(self,root):
# code for creating table
for i in range(total_rows):
for j in range(total_columns):
self.e = Button(root, width=20,text = lst[i][j])
self.e.grid(row=i, column=j)
# take the data
lst = [(1,'Raj','Mumbai',19),
(2,'Aaryan','Pune',18),
(3,'Vaishnavi','Mumbai',20),
(4,'Rachna','Mumbai',21),
(5,'Shubham','Delhi',21)]
# find total number of rows and
# columns in list
total_rows = len(lst)
total_columns = len(lst[0])
# create root window
root = Tk()
t = Table(root)
root.mainloop()
I've modified your code to include a Toplevel window called 'work' that gives access to the super buttons. The quantity of buttons is determined by the number of rows in table.
Each button is connected to another Toplevel window called 'edit'.
'Edit' contains Label, Entry and Button objects. The quantity is determined by the number of columns in table.
All windows are resizable with 'work' and 'edit' windows accessible via F1 key.
Escape key will close 'top' table via messagebox.
Ok this is the latest version.
I've divided the project into two python files, a data file called 'Datafile.py' and code called "Records.py'.
'Datafile.py' must be saved separately from 'Records.py.
Records code is completely configurable by changing table data in Datafile.py
Changing table data by adding or modifying columns and/or rows will configure the main program. The table, work and edit windows are determined by the row and columns in Datafile.py
SAVE THIS AS 'Datafile.py'
import time
timedate = time.localtime()
year = timedate.tm_year
indice = [ "gender", "name", "location", "age", "birth"]
table = [
["Male", "Raj", "Mumbai", 19, year-19],
["Male", "Aaryan", "Pune", 18, year-18],
["Female", "Vaishnavi","Mumbai", 20, year-20],
["Female", "Rachna", "Mumbai", 21, year-21],
["Male", "Shubham", "Delhi", 21, year-21],
["Male", "Roger", "New York", 17, year-17],
["Male", "David", "Sydney", 53, year-53],
["Female", "Jennifer", "London", 42, year-42]]
Here is the main program 'Records.py'.
SAVE AS 'Records.py'
import tkinter as tk
from tkinter import messagebox
from Datafile import timedate, indice, table
class Table:
def __init__(self, master):
self.master = root
self.master.title("Table")
# create table of buttons in self.store
self.rows = len(table)
self.columns = len(table[0])
# number of rows and columns
self.store = []
for r in range(self.rows):
self.store.append([])
for c in range(self.columns):
e = tk.Label(
self.master, width = 20, bd = 1,
relief = "raised", text = table[r][c])
e.grid(row = r, column = c, sticky = tk.NSEW)
self.flexx(self.master, r = r, c = c)
self.store[r].append(e) # all table buttons stored in list
self.master.geometry(f"{self.columns*100}x{self.rows*26}")
self.master.update()
# create toplevel widget for buttons
self.work = tk.Toplevel(self.master)
self.work.transient(self.master)
self.work.withdraw()
self.work.title("Individual Selector")
# populate toplevel widget with the Super buttons
# make buttons resizable
for r in range(self.rows):
e = tk.Button(
self.work, text = f"{table[r][1]}", bd = 1,
command = self.redirect(self.access, r))
e.grid(row=r, column=0, sticky = tk.NSEW)
self.flexx(self.work, r = r)
# Table edit window
self.edit = tk.Toplevel(self.master)
self.edit.transient(self.master)
self.edit.withdraw()
self.edit.title("Table Editor")
self.labelframe = tk.LabelFrame(self.edit, labelanchor = tk.N, text = "Name")
self.labelframe.grid(row = 0, column = 0, sticky = tk.NSEW)
self.flexx(self.edit)
# place entry tools here
self.data = []
for c in range(self.columns):
l = tk.Label(self.labelframe, text = f"{indice[c]}")
l.grid(row = c, column = 0, sticky = tk.NSEW)
e = tk.Entry(self.labelframe)
e.grid(row = c, column = 1, sticky = tk.NSEW)
self.data.append(e)
b = tk.Button(
self.labelframe, text = "Enter", bd = 1,
command = self.redirect(self.change, c))
b.grid(row = c, column = 2, sticky = tk.NSEW)
self.flexx(self.labelframe, r = c, c = 1)
# wire up master and work widgets
for a, b in [(self.master, self.closer),
(self.work, self.opener),
(self.edit, self.editor)]:
a.protocol('WM_DELETE_WINDOW', b)
a.bind("<KeyRelease-Escape>", b)
# Keys to work widget
self.master.bind("<F1>", self.opener)
self.work.bind("<F1>", self.opener)
self.edit.bind("<F1>", self.editor)
def redirect(self, f, *args):
return lambda: f(*args)
def flexx(self, m, r = 0, c = 0, rw = 1, cw = 1):
m.rowconfigure( r, weight = rw )
m.columnconfigure( c, weight = cw )
def change(self, c):
e = self.data[c]
self.store[self.currentRow][c]["text"] = e.get()
table[self.currentRow][c] = e.get()
def closer(self, ev = None):
if messagebox.askquestion(title = "Quit", message = "Confirm",
detail = "Close program?") == "yes":
self.master.destroy()
def opens( self, o, m ):
"""Toggle switch for toplevel"""
if o.winfo_viewable( ):
o.withdraw( )
m.focus_set( )
else:
o.focus_set( )
o.deiconify()
def position(self, o):
x = int(o.winfo_x() + o.winfo_width() + 5)
y = int(o.winfo_y() + o.winfo_height() / 2)
return x, y
def opener(self, ev = None):
x, y = self.position( self.master)
self.work.geometry(f"190x{26*self.rows}+{x}+{y}")
self.opens(self.work, self.master)
def editor(self, ev = None):
x, y = self.position( self.work)
self.edit.geometry(f"213x{26*self.columns}+{x}+{y}")
self.opens(self.edit, self.work)
def access(self, *args):
self.currentRow = r = args[0]
self.labelframe["text"] = f"{table[r][1]}"
for i,e in enumerate(self.data):
e.delete(0, "end")
e.insert(0, table[r][i])
self.editor()
if __name__ == "__main__":
root = tk.Tk()
top = Table(root)
root.mainloop()
I have code below :
var = DoubleVar()
scale = Scale(root, variable = var, command=self.scalfunc, from_=4, to=40, width=40,tickinterval=0,orient=VERTICAL,length=300,highlightthickness=0, background='#333333', fg='grey', troughcolor='#333333', activebackground='#1065BF')
scale.pack(anchor=CENTER)
scale.place(x=SCwidth/1.2, y=SCheight/15)
and I have button UNDO and anther button REDO
I want when I click in that button I make the Slider Value undo or redo
This code enables the use of Control keys to change the master colors of a tkinter.tk window.
Changes to Scale are stored in list called memory using index int called counter.
Control-z will undo
Control-Z will redo
Control-x will clear the memory
It shouldn't be too difficult to modify it for your needs.
import tkinter as tk
class UndoRedo:
def __init__(self):
self.master = tk.Tk()
self.master.withdraw()
self.master.columnconfigure(0, weight = 1)
self.master.columnconfigure(1, weight = 1)
self.color = 15790320 # #f0f0f0 = SystemButtonFace
self.var = tk.IntVar(self.master, value = self.color)
self.label = tk.Label(self.master, anchor = tk.E)
self.label.grid(row = 0, column = 0, sticky = tk.NSEW)
self.clear() # define memory and counter
self.scroll = tk.Scale(
self.master, orient = "horizontal", resolution = 65793,
label = "Brightness Control", from_ = 0, to = 16777215,
variable = self.var, command = self.control)
self.scroll.grid(row = 1, column = 0, columnspan = 2, sticky = tk.EW)
for k, v in [
( "<Control-z>", self.undo ),
( "<Control-Z>", self.redo ),
( "<Control-x>", self.clear )]:
self.master.bind(k, v)
self.master.geometry("400x86")
self.master.update()
self.master.minsize(329, 86)
self.master.resizable(True, False)
self.master.deiconify()
def display(self):
col = "#" + ("000000" + hex(self.color)[2:])[~5:]
self.var.set(self.color)
self.label["text"] = col
self.master.tk_setPalette(col)
def control(self, n):
self.color = int(n)
self.display()
if self.color not in self.memory:
self.memory.append(self.color)
self.counter = self.counter + 1
def action(self):
self.color = self.memory[self.counter]
self.display()
def undo(self, ev = None):
if self.memory:
self.counter = max(0, self.counter - 1)
self.action()
def redo(self, ev = None):
if self.memory:
self.counter = min(len( self.memory ) - 1, self.counter + 1)
self.action()
def clear(self, ev = None):
self.memory, self.counter = [], 0
self.label["text"] = "cleared"
if __name__ == "__main__":
bright = UndoRedo()
bright.master.mainloop()
If I understand correctly, you could store all positions taken by the slider in a list and use an index pointer to manipulate where it is in the list.
silder_ind = 0
slider_positions = [4] # Or wherever you want to start by default
When the user changes the position of the slider
new_pos = slider.get_current_value() # whatever the appropriate API call is to get the current value
slider_positions.append(new_pos)
slider_ind += 1
When the user hits undo
if (slider_ind - 1) >= 0:
slider_ind -= 1
redo
if (slider_ind + 1) < len(slider_positions):
slider_ind += 1
I have an appliaction which asks the user questions which they should answer.
The application uses pyttsx3 (version 2.9) to generate speech and I would like that while it speaks and only then, a gif animation would play for as long as it speaks.
How can this be accomplished?
For example:
import pyttsx3
engine = pyttsx3.init()
engine.say("I will speak this text")
engine.runAndWait()
And i want the gif animation to start running while runAndWait runs and stop when it stops
Ok I've found another program that works with the GIF images I have.
This program speaks for itself and displays a gif image.
I'm using tkinter Text object for output and a Label to hold the image.
It should allow GIF animes to run but it requires specific information about the GIF.
This runs on windows 10 and pyttsx3 - 2.5
"""
Program outputs text while voice is speaking.
This program works on Python 2.6.14 and Python 3.7.3
"""
import tkinter as tk
from tkinter import filedialog as fido
import random, pyttsx3
class chatter:
terminate = False
def showText( self, m ):
"""talk.showText( m )"""
self.main.insert( "insert", m )
self.master.update( )
self.main.edit_separator( )
def Start( self, name ) ->"":
"""talk.Start( name )"""
self.sentence = name.split( )
def Use( self, name, location, length ) ->"":
"""talk.Use( name, location, length )"""
name = self.sentence.pop( 0 )
self.showText( f"{name} " )
def End( self, name, completed ) ->"":
"""talk.End( name, completed )"""
if len( self.sentence ) > 0:
while len( self.sentence ) > 0:
name = self.sentence.pop( 0 )
self.showText( f"{name}\n" )
else:
self.showText( "\n" )
def Error( self, name, exception ) ->"":
"""talk.Error( name, exception )"""
self.terminate = True
self.engine.stop( )
self.showText( f"\n{name}\n{exception}\n" )
def say( self, m ):
"""talk.say( m )"""
self.engine.say( m, m )
self.engine.runAndWait( )
def intro( self ):
for m in [
"This program works on Python 2.6 and Python 3",
"I am PYTTSX version 2.5" ]:
talk.say( m )
self.master.after( 100, self.imageUpdate, 0 )
def displayInCanvas( self ):
"""talk.displayInCanvas( )"""
self.image = fido.askopenfilename( title = 'Pick Gif' )
w, h = self.image.width() + 14, self.image.height() + 46
self.item = self.canvas.create_image( 2,2, anchor = "nw" )
self.canvas.itemconfig( self.item, image = self.image )
self.master.geometry( f"{w}x{h}" )
self.labelframe[ "text" ] = self.image
def getImage( self ):
"""talk.getImage( )"""
self.item = tk.Label( anchor = "nw" )
self.item.grid( row=0, column=1, sticky="nsew" )
self.image = fido.askopenfilename( title = 'Pick Gif' )
# This requires specific knowledge about the GIF image
# n = number of animation frames
# self.frame = [ tk.PhotoImage( master = self.master, file=self.image, format = 'gif -index %i' %( i ) ) for i in range( n ) ]
self.frame = [ tk.PhotoImage(
master = self.master, file = self.image, format = "gif -index 0" ) ]
self.item.configure( image = self.frame[ 0 ] )
def imageUpdate( self, ind ):
"""imageUpdate( ind )"""
frame = self.frame[ ind ]
ind += 1
print( ind )
# Will play gif infinitely
if ind > 0:
ind = 0
self.item.configure( image = frame )
self.master.after( 100, self.imageUpdate, ind )
def closer( self, ev = None ):
self.master.destroy( )
def __init__( self ):
"""talk.init"""
self.engine = pyttsx3.init( )
self.startUtter = self.engine.connect(
"started-utterance", self.Start )
self.wordsUtter = self.engine.connect(
"started-word", self.Use )
self.endsUtter = self.engine.connect(
"finished-utterance", self.End )
self.erraUtter = self.engine.connect(
"error", self.Error )
self.persona = self.engine.getProperty( "voices" )[~0].id
self.engine.setProperty( "voice", self.persona )
for a,b in [ ("rate", 150 ), ( "volume", 0.25 ) ]:
self.engine.setProperty( a,b )
self.engine.runAndWait( )
self.master = tk.Tk()
self.main = tk.Text(
self.master, undo =1, wrap = "word",
block = 1, width = 80, height = 25 )
self.main.grid( row = 0, column = 0, sticky = "nsew" )
self.master.bind( "<Escape>", self.closer )
self.showText( __doc__ + "\n" )
self.main.update_idletasks( )
self.getImage( ) # load and display gif
self.main.focus_set()
self.master.after( 100, self.intro )
if __name__ == "__main__":
talk = chatter( )
tk.mainloop()
I am trying to create a simple drawing programme with a button that clears the canvas by deleting all shapes on it, however i am trying to use the delete() command that is built into tkinter but when i try and run it it says that delete is not defined.`What am I doing wrong?
from Tkinter import *
import Tkinter
root = Tk()
class PaintBox( Frame ):
def __init__( self ):
Frame.__init__( self )
self.pack( expand = YES, fill = BOTH )
self.master.title( "Color Draw v0.2.63.23 open closed beta (Still in Alpha) greenlight edition" )
self.master.geometry( "600x600" )
self.message = Label( self, text = "Drag the mouse SLOWLY to draw" )
self.message.pack( side = TOP )
self.myCanvas = Canvas( self )
self.myCanvas.pack( expand = YES, fill = BOTH )
self.bd = (2)
self.myCanvas.bind( "<B1-Motion>", self.paint )
def paint( self, event ):
x1, y1 = ( event.x - 4 ), ( event.y - 4 )
x2, y2 = ( event.x + 4 ), ( event.y + 4 )
self.myCanvas.create_rectangle( x1, y1, x2, y2, fill = "black", tags="box")
button = Button(root, text = "Clear", command = delete(box))
button.grid(row = 1, column = 0)
PaintBox().mainloop()
Here is the full traceback #mgilson:
ERROR: execution aborted
In [2]: %run C:/Users/en58522/Downloads/paintcolor.txt---------------------------------------------------------------------------
NameError Traceback (most recent call last)
C:\Program Files\Enthought\Canopy\App\appdata\canopy-1.4.1.1975.win-x86_64\lib\site-packages\IPython\utils\py3compat.pyc in execfile(fname, glob, loc)
195 else:
196 filename = fname
--> 197 exec compile(scripttext, filename, 'exec') in glob, loc
198 else:
199 def execfile(fname, *where):
C:\Users\en58522\Downloads\paintcolor.txt in <module>()
3 root = Tk()
4
----> 5 class PaintBox( Frame ):
6 def __init__( self ):
7 Frame.__init__( self )
C:\Users\en58522\Downloads\paintcolor.txt in PaintBox()
24 self.myCanvas.create_rectangle( x1, y1, x2, y2, fill = "black", tags="box")
25
---> 26 button = Button(root, text = "Clear", command = delete(box))
27 button.grid(row = 1, column = 0)
28
NameError: name 'delete' is not defined
Here is some working code to get you started:
from Tkinter import *
import Tkinter
root = Tk()
class PaintBox( Frame ):
def __init__( self, root ):
Frame.__init__( self, root )
self.pack( expand = YES, fill = BOTH )
self.master.title( "Color Draw v0.2.63.23 open closed beta (Still in Alpha) greenlight edition" )
self.master.geometry( "600x600" )
self.message = Label( self, text = "Drag the mouse SLOWLY to draw" )
self.message.pack( side = TOP )
self.myCanvas = Canvas( self )
self.myCanvas.pack( expand = YES, fill = BOTH )
self.bd = (2)
self.myCanvas.bind( "<B1-Motion>", self.paint )
button = Button(root, text = "Clear", command = self.delete)
button.pack()
def paint( self, event ):
x1, y1 = ( event.x - 4 ), ( event.y - 4 )
x2, y2 = ( event.x + 4 ), ( event.y + 4 )
self.myCanvas.create_rectangle( x1, y1, x2, y2, fill = "black", tags="box")
def delete(self):
items = self.myCanvas.find_all()
for item in items:
self.myCanvas.delete(item)
PaintBox(root).mainloop()
I've tried to stay true to your original code as much as possible.
A few things to notice...
I .pack the button because using .grid and .pack in the same widget doesn't work.
I defined a delete function that clears the grid of the box that you input. I defined it in the class's __init__.
The command = ... should be a function. Your original code attempted to make a function call in the command = ... bit. This is a common mistake and leads to the function being executed when you create the button and then never again. I used a bound method because bound methods get self passed implicitly.