Convert string into a Tkinter notebook frame - string

Ok so I am trying to find the frame which Tkinter is using, then take its width and height and resize the window so that everything fits nicely without ugly spaces left. So far I have gotten the following...
convert = {"tab1_name", "tab1"; "tab2_name", "tab2"; "tab3_name", "tab3") ##(it goes on)
a = mainframe.tab(mainframe.select(), "text")
b = convert[a]
w = b.winfo_reqwidth()
h = b.winfo_reqheight()
mainframe.configure(width=w, height=h)
The names of each frame in the notebook are tab1, tab2, tab3, etc., but the labels on them are unique because they describe what happens in the tab. I want to be able to take the string returned from the convert dictionary function and use it as the frame's name. I am not sure if the frame is a class or what else. Is there a way to convert the string b into the frame's name and somehow use it in the .winfo_reqheight()? I do not want to have to make a thing which says...
if b=="tab1":
w = tab1.winfo_reqwidth()
h = tab1.winfo_reqheight()
mainframe.configure(width=w, height=h)
for each frame because I want it to be easy to add new frames without having to add so much code.
Thank you

Option 1:
You can store actual objects in dictionaries. So try:
convert = {"tab1_name": tab1, "tab2_name": tab2, "tab3_name": tab3}
a = mainframe.tab(mainframe.select(), "text")
b = convert[a]
w = b.winfo_reqwidth()
h = b.winfo_reqheight()
mainframe.configure(width=w, height=h)
Option 2:
Executing strings is possible with the 'exec('arbitrary code in a string')' function
See How do I execute a string containing Python code in Python?.
You could do this: (with just text in the dictionary or whatever convert is)
convert = {"tab1_name": "tab1", "tab2_name": "tab2", "tab3_name": "tab3"}
a = mainframe.tab(mainframe.select(), "text")
b = convert[a]
code1 = "w = %s.winfo_reqwidth()" % b
code2 = "h = %s.winfo_reqheight()" % b
exec(code1) # for python 2 it is: exec code1
exec(code2) # python 3 changed the exec statement to a function
mainframe.configure(width=w, height=h)
Be careful that you don't let malicious code into the exec statement, because python will run it. This is usually only a problem if an end user can input things into the function(it sounds like you don't have to worry about this).
btw, I think your first line is incorrect. You open with a { but close with ). Proper dictionary syntax would be:
convert = {"tab1_name": "tab1", "tab2_name": "tab2", "tab3_name": "tab3"}
Notice the colons separating key and value, and commas in-between entries.

Related

Return the reference of Tkinter control variable

I am not an experienced programmer so advance apologies if I am asking a basic question. I am using a for loop to print tkinter Entries and trying to return the reference of these Entries to the calling function. But when I try to get the text typed in the entry it always returns empty string? So my question is whether it is possible to return the reference of the control variable in python ? or I am using a wrong approach?
def data_entry_txtfield(self,rn,cn,pu,pd):
# Creates the Entry to enter data - rn is the row and cn is column
# pd and pu are padding up and padding down
entry = tk.StringVar()
tk.Entry(self.inputlab,width=32,bg=entrycolor,textvariable=entry)
entry.grid(column=cn,row=rn,pady=(pu,pd))
return entry
tbtlocationentry = self.data_entry_txtfield(9,4,0,12)
text = tbtlocationentry.get()
print(text)`
There are a lot of errors in this code like the bottom line your using self.data_entry_txtfield to call the function but you dont need the self. unless your using in a class. Also the entry needs a variable name to be able to change stuff inside it. I've fixed some errors and here is the code
def data_entry_txtfield(self,rn,cn,pu,pd):
# Creates the Entry to enter data - rn is the row and cn is column
# pd and pu are padding up and padding down
entry = tk.StringVar()
ety = tk.Entry(self.inputlab,width=32,bg=entrycolor,textvariable=entry)
ety.grid(column=cn,row=rn,pady=(pu,pd))
return entry
tbtlocationentry = self.data_entry_txtfield(9,4,0,12)
text = tbtlocationentry.get()
print(text)
I've left in the self. because i'm assuming your using this inside a class

How do I store the value of an indexed list in a global variable and call at it through a formatted function?

How do I store the value of an index and then use that value in a formatted exec function to print me the second results of each list under class Animal(): Dog list, which is what I expect to print. A simplified version of the essence of my problem along with further clarification below:
class Global():
str_list = []
current_word = ""
adj_word = 'poofy'
adj_int = 0
size = 0
pounds = 0
dog_years = 0
class Animal():
##### Formatted like so:[[visual size],[pounds],[age in dog years],[almost dead]] #####
dog = [['small', 'poofy'],[7, 45],[18, 101],[0, 1]]
input = 'dog'
def done():
print(Global.adj_int)
print(str(Global.size), str(Global.pounds), str(Global.dog_years))
def split_str():
Global.str_list = input.split()
split_str()
def analyze():
Global.current_word = Global.str_list.pop(0)
exec(f"""if Global.adj_word in Animal.{Global.current_word}[0]:
Global.adj_int = Animal.{Global.current_word}[0].index('{Global.adj_word}')
Global.size = Animal.{Global.current_word}[1][{Global.adj_int}]
Global.pounds = Animal.{Global.current_word}[2][{Global.adj_int}]
Global.dog_years = Animal.{Global.current_word}[3][{Global.adj_int}]""")
if len(Global.str_list) == 0:
done()
analyze()
it returns:
1
7 18 0
When I expect it to return "45 101 1" for size, pounds, dog_years because I am storing the .index value of 'poofy' for Animal.dog list in Global.adj_int. which in this case is '1'. Then I try to format the code so it uses that value to print the second values of each list but for some reason it will not print the expected results(under def analyze():... exec(f""".... Does anyone have an answer to this question?? This is a much more simple version of what I originally have but produces the exact same result. when I try to use the formatted code it acts as if adj_int = 0 when really it's adj_int = 1 (and I know it is stored as 1 like it should be because I print adj_int at the end to check) or I am not able to format the code in this way? But I need a work around regardless.
The problem is that the string argument to exec is being evaluated before it is executed. So, when you are calling exec this is what is called:
exec(f"""if Global.adj_word in Animal.dog[0]:
Global.adj_int = Animal.{dog}[0].index('poofy')
Global.size = Animal.dog[1][0]
Global.pounds = Animal.dog[2][0]
Global.dog_years = Animal.dog[3][0]""")
And after this, Global.adj_int becomes 1. The control flow and the structure of your code is incredibly complex comparing to its simplicity so I would carefully rethink its design, but for a quick fix, you probably want to first execute the part that sets the adj_int and then the rest, like this:
exec(f"""if Global.adj_word in Animal.{Global.current_word}[0]:
Global.adj_int = Animal.{Global.current_word}[0].index('{Global.adj_word}'"""))
exec(f"""if Global.adj_word in Animal.{Global.current_word}[0]:
Global.size = Animal.{Global.current_word}[1][{Global.adj_int}]
Global.pounds = Animal.{Global.current_word}[2][{Global.adj_int}]
Global.dog_years = Animal.{Global.current_word}[3][{Global.adj_int}]""")
Even after stating that your program is unnecessarily complex, let me additionally point out that using a global, mutable state is a really really bad practice, which makes your programs hard to follow, maintain & debug. The same concern relates to using exec.

Creating a display using tkinter and labels - how to run two things at the same time

I am creating a display using a tkinter label. I have lists of variables established for every line of "pixels", aka:
Line1 = []
Line2 = []
So that the number of lines is also the height of the display. The width of the display is the number of characters in each line, which I have added like this:
A = range(1, 311)
for b in A:
i = " "
Line1.append(i)
Line2.append(i)
I then transform the empty spaces into empty spaces which will actually print like empty spaces (I do not really understand this part since I got it from a question that I asked here, but it works, and I am happy it does) ...
LLine1 = ''.join(map(str, Line1))
LLine2 = ''.join(map(str, P2))
And finally, I "display" the display using a label:
Display = tkinter.Label(window, text = (LLine1, "\n", LLine2)
Up to here, every think should work properly. Now here comes the problem. In order for the display to show images, I need my program to change the variables and at the same time configure the label - possibly using... eeh... queues(?)... multi-threading(??)... multi-processing(???)? Basically, I need something like this:
threads.append(threading.Thread(target = Start_tkinter))
threads.append(threading.Thread(target = Start_running))
map(lambda x: x.start(), threads)
Where "Start_tkinter" configures the label sixty times per second using this (which will be in a loop):
window.mainloop() //this part will obviously not be a part of the loop
LLine1 = ''.join(map(str, Line1)) //But all this will be in the loop
LLine2 = ''.join(map(str, Line2))
Display.config(text = (PP1, PP2))
Display.after(16, count)
And "Start_running" changes the variables (which will also be in a loop). However, this does not work. So... what is the problem? I know that I have almost no experience, so and tips on any part of this programs are VERY appreciated :)
About your question with changing variables i may choose tkinter StringVar
import tkinter as Tk
root=Tk.Tk()
Var=Tk.StringVar()
Var.set("Some string")
print(Var.get())
Label=Tk.Label(root, text=Var.get())
Label.pack()
root.mainloop()
you can insert text by command set()
Var.set("Some string")
and insert to tkinter lable by get()
Label=Tk.Label(root, text=Var.get())
Label.pack()
and the label may change too.
I think this is the one of best solutions because you donĀ“t need to change Labels, but only variables, which is much faster.On the other hand you need same number of variables as your resolution.

How can I convert a string into a function in Python?

I have to deal with csv image data from a camera which exports the data with a header. In that header is a simple function for converting CCD counts into power density. This equation includes both the dark offset level as well as a calibration factor. Here is an example from one line of an image file:
Power Density,=,(n - 232) * 4.182e-005 W/cm^2
Notice the commas. The csv header can be expected to have the same structure each time with different constants for dark level (232) and power density conversion (4.182e-005).
What I would like to be able to do is grab the last cell, strip off the units at the end (W/cm^2), and use what is left to define a function in Python. Something like
f = lambda n: '(n - 232) * 4.182e-005'
Is it possible to do so? If so, how?
eval and exec, which use compile, are both ways to dynamically convert code as text to a compiled function. If you dynamically create a new function, you only need to do the conversion once.
row = "Power Density,=,(n - 232) * 4.182e-005 W/cm^2".split(',')
expr = row[2].replace( ' W/cm^2', '')
# f = eval("lambda n:" + expr) # based on your original idea
exec("def f(n): return " + expr) # more flexible
print(f(0))
# -0.00970224
The lambda eval and def exec have the same result, other than f.name, but as usual, the def form is more flexible, even if the flexibility is not needed here.
The usual caveats about executing untrusted code apply. If you are working with photo files not your own and were worried about an adversary feeding you a poisoned file, then indeed you might want to tokenize expr and check that is only has the tokens expected.
I found a way to do it using eval, but I expect that it isn't very pythonic so I would still be interested in seeing other answers.
Here row is the row of interest from a csv.reader object, i.e. the same string I posted in the question divided at the commas.
# Strip the units from the string
strng = row[2].replace( ' W/cm^2', '')
# Define a function based on the string
def f( n):
return eval( strng)
# Evaluate a value
print( f( 0))
# Returns: -0.00970224

Matplotlib - Stacked Bar Chart with ~1000 Bars

Background:
I'm working on a program to show a 2d cross section of 3d data. The data is stored in a simple text csv file in the format x, y, z1, z2, z3, etc. I take a start and end point and flick through the dataset (~110,000 lines) to create a line of points between these two locations, and dump them into an array. This works fine, and fairly quickly (takes about 0.3 seconds). To then display this line, I've been creating a matplotlib stacked bar chart. However, the total run time of the program is about 5.5 seconds. I've narrowed the bulk of it (3 seconds worth) down to the code below.
'values' is an array with the x, y and z values plus a leading identifier, which isn't used in this part of the code. The first plt.bar is plotting the bar sections, and the second is used to create an arbitrary floor of -2000. In order to generate a continuous looking section, I'm using an interval between each bar of zero.
import matplotlib.pyplot as plt
for values in crossSection:
prevNum = None
layerColour = None
if values != None:
for i in range(3, len(values)):
if values[i] != 'n':
num = float(values[i].strip())
if prevNum != None:
plt.bar(spacing, prevNum-num, width=interval, \
bottom=num, color=layerColour, \
edgecolor=None, linewidth=0)
prevNum = num
layerColour = layerParams[i].strip()
if prevNum != None:
plt.bar(spacing, prevNum+2000, width=interval, bottom=-2000, \
color=layerColour, linewidth=0)
spacing += interval
I'm sure there's a more efficient way to do this, but I'm new to Matplotlib and still unfamilar with its capabilities. The other main use of time in the code is:
plt.savefig('output.png')
which takes about a second, but I figure this is to be expected to save the file and I can't do anything about it.
Question:
Is there a faster way of generating the same output (a stacked bar chart or something that looks like one) by using plt.bar() better, or a different Matplotlib function?
EDIT:
I forgot to mention in the original post that I'm using Python 3.2.3 and Matplotlib 1.2.0
Leaving this here in case someone runs into the same problem...
While not exactly the same as using bar(), with a sufficiently large dataset (large enough that using bar() takes a few seconds) the results are indistinguishable from stackplot(). If I sort the data into layers using the method given by tcaswell and feed it into stackplot() the chart is created in 0.2 seconds, rather than 3 seconds.
EDIT
Code provided by tcaswell to turn the data into layers:
accum_values = []
for values in crosssection:
accum_values.append([float(v.strip()) for v iv values[3:]])
accum_values = np.vstack(accum_values).T
layer_params = [l.strip() for l in layerParams]
bottom = numpy.zeros(accum_values[0].shape)
It looks like you are drawing each bar, you can pass sequences to bar (see this example)
I think something like:
accum_values = []
for values in crosssection:
accum_values.append([float(v.strip()) for v iv values[3:]])
accum_values = np.vstack(accum_values).T
layer_params = [l.strip() for l in layerParams]
bottom = numpy.zeros(accum_values[0].shape)
ax = plt.gca()
spacing = interval*numpy.arange(len(accum_values[0]))
for data,color is zip(accum_values,layer_params):
ax.bar(spacing,data,bottom=bottom,color=color,linewidth=0,width=interval)
bottom += data
will be faster (because each call to bar creates one BarContainer and I suspect the source of your issues is you were creating one for each bar, instead of one for each layer).
I don't really understand what you are doing with the bars that have tops below their bottoms, so I didn't try to implement that, so you will have to adapt this a bit.

Resources