Python Tkinter dependent drop down - python

i trying to do the following:
i have two lists, matching each other. i want to create a dropdown list with tkinter with one list, and return the match from the other list. the values in the lists match injectivly one to each other.
i want the second list match to be a label or entry without choosing it like with two dependent drop down list. i've tried the following, but it only return OD.
thanks from advance
Avraham
import tkinter
from tkinter import ttk
root = tkinter.Tk()
options = tkinter.StringVar(root)
pipe_size = [
'1/8', '1/4', '3/8', '1/2', '3/4', '1', '1 1/4', '1 1/2', '2', '2 1/2', '3', '3',
'1/2', '4', '5', '6', '8', '10', '12', '14', '16', '18', '20', '22', '24', '26', '28', '30',
'32', '34', '36', '38', '40', '42', '44', '46', '48', '52', '56', '60'
]
OD_mm = [
'10.3', '13.7', '17.1', '21.3', '26.7', '33.4', '42.2', '48.3', '60.3', '73', '88.9',
'101.6', '114.3', '141.3', '168.3', '219.1', '273.1', '323.9', '355.6', '406.4', '457.2',
'508',
'559', '610', '660', '711', '762', '813', '884', '914', '965', '1016', '1067', '1118', '1168',
'1219', '1321'
]
str_out=tkinter.StringVar(root)
str_out.set("OD")
def callback(eventObject):
abc = eventObject.widget.get()
car = pipe_size_list.get()
index=pipe_size.index(car)
str_out.config(values=OD_mm[index])
str_out.set(OD_mm_str.get())
pipe_size_list = ttk.Combobox(root, width=37, value=(pipe_size))
pipe_size_list.grid(row=3, column=1, columnspan=2, padx=10, pady=2, sticky='w')
OD_mm_str = tkinter.Label(root, textvariable = str_out)
OD_mm_str.grid(row = 5, column = 1)
OD_mm_str.bind('<<ComboboxSelected>>', callback)
root.mainloop()

Related

Middle + Center Alignment on Plotly

Are we able to do center + middle align text in Tables? In the documentation, I’ve only managed to find the option to horizontally center text, and couldn't find any post with this topic.
What I'm expecting is (I edited the picture so it will be center + middle aligned:
What i got:
And if you're wondering what's my code, this is:
import plotly.graph_objects as go
layout = dict(
paper_bgcolor='rgba(0,0,0,0)',
plot_bgcolor='rgba(0,0,0,0)'
)
header = ['LVL', 'ATK', 'Elemental Mastery']
cells = [
['1', '10', '20', '30', '40', '50', '60', '70', '80', '90'],
['42', '77', '135', '170', '231', '292', '353', '414', '475', '510'],
['36', '49', '64', '78', '93', '107', '122', '136', '151', '165']
]
fig = go.Figure(layout=layout, data=[go.Table(
header=dict(
values=header,
line_color='#3498DB',
fill_color='rgba(0,0,0,0)',
align='center',
line=dict(width=2.5),
font=dict(color='#3498DB', size=11.5)
),
cells=dict(
values=cells,
line_color='#3498DB',
fill_color='rgba(0,0,0,0)',
align='center',
height=30,
line=dict(width=2.5),
font=dict(color='#3498DB', size=13.5))
),
])
fig.update_layout(
width=340, height=410 if len(header[-1:][0].split(' ')[0]) > 5 else 390,
margin=dict(l=30, r=30, b=30, t=30)
)
fig.show()

Why am I getting error after looping into file and trying to join the list

I have large text file which has numbers. I want to loop through the file and append the numbers to the list_of_numbers. Problem is that the loop appends to the list but not in the way I want, that's why the list looks like this after iteration
['\n', '+', '1', '6', '1', '0', '8', '5', '0', '7', '7', '6', '4', '\n', '+', '1', '6', '1', '0', '7', '6', '4', '6', '0', '2', '9', '\n', '+', '1', '6', '1', '0', '7', '6', '4', '6', '8', '4', '6', '\n', '+', '1', '6', '1', '0', '8', '5', '0', '5', '9', '3', '4', '\n', '+', '1', '6', '1', '0', '7', '6', '4', '0', '7', '8', '3', '\n', '+', '1', '6', '1', '0', '7', '6', '4', '9', '2', '8', '2', '\n', '+', '1', '6', '1', '0', '7', '6', '4', '0', '0', '4', '9', '\n']
this is just part of the output. I want this to be in this type [123455334,492023232,32322323]
I tried to do this but it does not work and gets errors
print(list([int(x) for x in ''.join(list_of_numbers).split('\n')]))
here is my full code
from tkinter import *
from tkinter import filedialog
import selenium
import time
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium import webdriver
from selenium import webdriver
list_of_numbers=[]
full_list_of_numbers=[]
def openFile():
tf = filedialog.askopenfilename(
initialdir="C:/Users/MainFrame/Desktop/",
title="Open Text file",
filetypes=(("Text Files", "*.txt"),)
)
pathh.insert(END, tf)
tf = open(tf) # or tf = open(tf, 'r')
data = tf.read()
txtarea.insert(END, data)
tf.close()
for i in data:
list_of_numbers.append(i)
print(list_of_numbers)
ws = Tk()
ws.title("PythonGuides")
ws.geometry("400x450")
ws['bg']='#fb0'
txtarea = Text(ws, width=40, height=20)
txtarea.pack(pady=20)
pathh = Entry(ws)
pathh.pack(side=LEFT, expand=True, fill=X, padx=20)
Button(
ws,
text="Open File",
command=openFile
).pack(side=RIGHT, expand=True, fill=X, padx=20)
ws.mainloop()
print(list_of_numbers)
while ' ' in list_of_numbers:
list_of_numbers.remove(' ')
print(list([int(x) for x in ''.join(list_of_numbers).split('\n')]))
Look at that part
tf = open(tf) # or tf = open(tf, 'r')
data = tf.read()
txtarea.insert(END, data)
tf.close()
for i in data:
list_of_numbers.append(i)
data is one big string. Then you iterate over it one char at a time and append that single char (incl, '+' and '\n' to the list. So you get what you get.
Replace the above snippet with following:
with open(tf) as f: # use context manager
for line in f:
txtarea.insert(END, line)
list_of_numbers.append(int(line))
Note, this assumes there are no empty lines in your file. If there are, then
with open(tf) as f: # use context manager
for line in f:
txtarea.insert(END, line)
line = line.strip()
if line:
list_of_numbers.append(int(line))

How to save dictionary value into MySQL database tkinter python?

I want to save the value stored in dictionary into MySQL database from tkinter GUI The dictionary of value is as follows. Currently
I am using if else statement for each length which is very lengthy.
db=storage.connect()
cursor=db.cursor()
sd1={'AWB Fees': ('122', 'Rupees', '1', '34', '4', '136.00', '8', '10.88'),
'Agency Charges': ('122', 'Rupees', '1', '78', '4', '312.00', '8',
'24.96'),'AWB': ('122', 'Rupees', '1', '34', '4', '136.00', '8',
'10.88')}
length=len(sd1)
y = (sd1.keys())
if (length==1):
za = ((tuple(y))[0])
xa = ((sd1[za])[0])
xb = ((sd1[za])[1])
xc = ((sd1[za])[2])
xd = ((sd1[za])[3])
xf = ((sd1[za])[4])
xg = ((sd1[za])[5])
xh = ((sd1[za])[6])
xi = ((sd1[za])[7])
cursor.execute(
"INSERT INTO sea_exp_tra_raiselocal_inv_fright_est
(rlcn_id,billing_head, sac, currency, ex_rate, rate, value,
amount, gst, gst_amnt) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)",
(sepmId,za,xa,xb,xc,xd,xf,xg,xh,xi))
else:
pass
db.commit()
db.close()
You're really overcomplicating things. First lets clean up the code:
sd1 = {
'AWB Fees': ('122', 'Rupees', '1', '34', '4', '136.00', '8', '10.88'),
'Agency Charges': ('122', 'Rupees', '1', '78', '4', '312.00', '8', '24.96'),
'AWB': ('122', 'Rupees', '1', '34', '4', '136.00', '8', '10.88')
}
sql = """
INSERT INTO sea_exp_tra_raiselocal_inv_fright_est
(rlcn_id,billing_head, sac, currency, ex_rate, rate, value, amount, gst, gst_amnt)
VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)
"""
key = list(d.keys())[0]
values = (sepmId, key,) + sd1[key]
cursor.execute(sql, values)
now all you have to do is to iterate over sd1.items() (which yields key,value pairs):
sd1 = {
'AWB Fees': ('122', 'Rupees', '1', '34', '4', '136.00', '8', '10.88'),
'Agency Charges': ('122', 'Rupees', '1', '78', '4', '312.00', '8', '24.96'),
'AWB': ('122', 'Rupees', '1', '34', '4', '136.00', '8', '10.88')
}
sql = """
INSERT INTO sea_exp_tra_raiselocal_inv_fright_est
(rlcn_id,billing_head, sac, currency, ex_rate, rate, value, amount, gst, gst_amnt)
VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)
"""
for key, val in sd1.items():
values = (sepmId, key,) + val
cursor.execute(sql, values)

How to customize a table for label printing with reportlab

I want to make the label print to PDF from reportlab by python 3.6, and I checked the reportlab for tables' usage. All of the methods are regular form.
I want to merge the cell to realize the final effect as follows.
<> contains records from database.
My label requirements
By "Span" method, I got the tables here:
When I met the last rows, I cannot split it. Because I use 0.5cm x16, 0.5cmx11 to format the table. Now, should I change it to 0.25 cmx32, 0.25cm x 22? It must be a mass work.
My result
Does anyone give me a suggestion to solve this problem? I need a direction. Thanks.
* If simply draw line and output text, I cannot realize the align,valign, wrap etc.
My codes are here:
# -*- coding:utf-8 -*-
from reportlab.lib import colors
from reportlab.lib.pagesizes import A4,cm
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle
doc = SimpleDocTemplate("LabelTest.pdf", pagesize=A4)
# container for the 'Flowable' objects
elements = []
data= [['1', '', '','','','','','','', '', '', '', '13', '', '', ''],
['', '', '','','','','','','', '', '', '', '', '', '', ''],
['', '', '','','','','','','', '', '', '', '', '', '', ''],
['4','', '','','','','','','', '', '', '', '13', '14', '15', '16'],
['5', '', '','','','','','','7','8','9', '10', '11', '12', '13', '14', '15', '16'],
['6', '', '','','','','','','7','8','9', '10', '11', '12', '13', '14', '15', '16'],
['7', '', '','','','','','','7','8','9', '10', '11', '12', '13', '14', '15', '16'],
['8', '', '','','','','','','7','8','9', '10', '11', '12', '13', '14', '15', '16'],
['9', '', '','','','','7','8','9', '10', '11', '12', '13', '14', '15', '16'],
['', '', '','','','','','8','9', '10', '11', '12', '13', '14', '15', '16'],
['', '', '','','','','','8','9', '10', '11', '12', '13', '14', '15', '16']]
t=Table(data,16*[0.5*cm], 11*[0.5*cm],)
t.setStyle(TableStyle([
('GRID',(0,0),(-1,-1),1,colors.black),
('SPAN',(-4,0),(-1,3)), # Right corner for logo image
('SPAN',(0,0),(-5,2)), # First two rows for product des and surface
('SPAN',(0,3),(-5,3)), # Third row for product requirements
('SPAN',(0,4),(5,7)), # For product picture
('SPAN',(6,3),(-1,6)), # Description and size
('SPAN',(6,4),(-1,7)), # For mat'l no.
('SPAN',(0,8),(5,-1)), # EAN-13
]))
elements.append(t)
# write the document to disk
doc.build(elements)
Currently, I find a solution to make it by myself, maybe it is not the best one, but it really helps a lot.
# -*- coding:utf-8 -*-
from reportlab.lib import colors
from reportlab.lib.pagesizes import A4,cm
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle
doc = SimpleDocTemplate("ex09_1-ReyherTable.pdf", pagesize=A4)
# container for the 'Flowable' objects
elements = []
data0= [['1','9']]
t0=Table(data0,colWidths=[6*cm,2*cm],rowHeights=[2*cm])
t0.setStyle(TableStyle([
('GRID',(0,0),(-1,-1),1,colors.black),
]))
data1= [['2','9']]
t1=Table(data1,colWidths=[3*cm,5*cm],rowHeights=[2*cm])
t1.setStyle(TableStyle([
('GRID',(0,0),(-1,-1),1,colors.black),
]))
data2= [['3','4','5'],
['4','5','6'],]
t2=Table(data2,colWidths=[3*cm,2.5*cm,2.5*cm],rowHeights=2*[0.75*cm])
t2.setStyle(TableStyle([
('GRID',(0,0),(-1,-1),1,colors.black),
('SPAN',(0,0),(0,-1)),
('SPAN',(-1,0),(-1,-1)),
]))
elements.append(t0)
elements.append(t1)
elements.append(t2)
# write the document to disk
doc.build(elements)

Iteration & Casting index values to integer and float in nested lists

I'm having difficulty with iterating through the nested list table below. I understand how to iterate through the table once, but to go a level deeper and iterate through each nested list, I am stuck on the correct syntax to use. In iterating through the sublists, I am trying to cast each 'age' and 'years experience' to an integer, perform the operation 'age' - 'years experience', and append the value (as a string) to each sublist.
table = [
['first_name', 'last_name', 'age', 'years experience', 'salary'],
['James', 'Butt', '29', '8', '887174.4'],
['Josephine', 'Darakjy', '59', '39', '1051267.9'],
['Art', 'Venere', '22', '2', '47104.2'],
['Lenna', 'Paprocki', '33', '7', '343240.2'],
['Donette', 'Foller', '26', '2', '273541.4'],
['Simona', 'Morasca', '35', '15', '960967.0'],
['Mitsue', 'Tollner', '51', '31', '162776.7'],
['Leota', 'Dilliard', '64', '39', '464595.5'],
['Sage', 'Wieser', '27', '9', '819519.7'],
['Kris', 'Marrier', '59', '33', '327505.55000000005'],
['Minna', 'Amigon', '45', '23', '571227.05'],
['Abel', 'Maclead', '46', '23', '247927.25'],
['Kiley', 'Caldarera', '33', '7', '179182.8'],
['Graciela', 'Ruta', '48', '21', '136978.95'],
['Cammy', 'Albares', '29', '9', '1016378.95'],
['Mattie', 'Poquette', '39', '15', '86458.75'],
['Meaghan', 'Garufi', '21', '3', '260256.5'],
['Gladys', 'Rim', '52', '26', '827390.5'],
['Yuki', 'Whobrey', '32', '10', '652737.0'],
['Fletcher', 'Flosi', '59', '37', '954975.15']]
##Exercise 3 (rows as lists): Iterate over each row and append the following values:
#If it is the first row then extend it with the following ['Started Working', 'Salary / Experience']
#Start work age (age - years experience)
#Salary / Experience ratio = (salary / divided by experience)
for i, v in enumerate(table):
extension = ['Started Working', 'Salary/Experience']
if i == 0:
v.extend(extension)
print(i,v) #test to print out the index and nested list values
#for index, value in enumerate(v):
# age =
#exp =
#start_work = age - exp
#print(index, value) test to print out the index and each value in the nested list
Pass the argument start to enumerate, enumerate(table, 1) in your case,
table = [['first_name', 'last_name', 'age', 'years experience', 'salary'],
['James', 'Butt', '29', '8', '887174.4'],
['Josephine', 'Darakjy', '59', '39', '1051267.9'],
['Art', 'Venere', '22', '2', '47104.2']]
table[0].extend(['Started Working', 'Salary/Experience'])
for idx, row in enumerate(table[1:], 1):
start_work_age = int(row[2]) - int(row[3])
ratio = float(row[4]) / int(row[3])
table[idx].extend([str(start_work_age), str(ratio)])
print(table)
# Output
[['first_name', 'last_name', 'age', 'years experience', 'salary', 'Started Working', 'Salary/Experience'],
['James', 'Butt', '29', '8', '887174.4', '21', '110896.8'],
['Josephine', 'Darakjy', '59', '39', '1051267.9', '20', '26955.5871795'],
['Art', 'Venere', '22', '2', '47104.2', '20', '23552.1']]
If you can convert the space to an underscore in years experience you can use collections.namedtuple to make your life simpler:
from collections import namedtuple
table = [
['first_name', 'last_name', 'age', 'years_experience', 'salary'],
['James', 'Butt', '29', '8', '887174.4'],
['Josephine', 'Darakjy', '59', '39', '1051267.9'],
['Art', 'Venere', '22', '2', '47104.2'],
# ...
]
workerv1 = namedtuple('workerv1', ','.join(table[0]))
for i,v in enumerate(table):
worker = workerv1(*v)
if i == 0:
swage = 'Started Working'
sex_ratio = 'S/Ex ratio'
else:
swage = int(worker.age) - int(worker.years_experience)
sex_ratio = float(worker.salary) / float(worker.years_experience)
print("{w.first_name},{w.last_name},{w.age},{w.years_experience},{w.salary},{0},{1}".format(
swage, sex_ratio, w=worker))

Categories