tkinter ListBox and Label position - python

I am little bit comfused with grid system in tkinter Python. Can anyone show how to make it in right way?! ListBox and Label items positions are not in the places where I expexted to see them.
CODE:
self.third_label = Label(self, text="TEXT")
self.third_label.grid(row=2, column=0, columnspan=4, padx=10, pady=10, sticky=W)
self.fourth_label = Label(self, text="LONG TEXT")
self.fourth_label.grid(row=2, column=1, columnspan=4, padx=10, pady=10, sticky=W)
self.fifth_label = Label(self, text="SOME TEXT")
self.fifth_label.grid(row=2, column=2, columnspan=6, padx=10, pady=10, sticky=W)
self.sixth_label = Label(self, text="BIG TEXT")
self.sixth_label.grid(row=2, column=3, columnspan=4, padx=10, pady=10, sticky=W)
self.first_listbox = Listbox(self, width=40, selectmode=EXTENDED)
self.first_listbox.grid(row=3, column=0, columnspan=4, padx=10, pady=10, sticky=W)
self.second_listbox = Listbox(self, width=40, selectmode=EXTENDED)
self.second_listbox.grid(row=3, column=2, columnspan=4, padx=10, pady=10, sticky=W)
self.third_listbox = Listbox(self, width=40, selectmode=EXTENDED)
self.third_listbox.grid(row=3, column=4, columnspan=4, padx=10, pady=10, sticky=W)
self.fourth_listbox = Listbox(self, width=40, selectmode=EXTENDED)
self.fourth_listbox.grid(row=3, column=6, columnspan=4, padx=10, pady=10, sticky=W)
What I have right now:
Just Example:

The grid system works fine. The problem is your columnspans, which don't make much sense. You're gridding the widgets into certain column positions then giving them a columnspan that is beyond the range of where the next widget is to be gridded so on and so forth.
Small example:
import string
import tkinter as tk
root = tk.Tk()
for i in range(3):
tk.Label(root, text=string.ascii_letters).grid(row=0, column=i)
tk.Listbox(root, width=40).grid(row=1, column=i)
root.mainloop()
Edit from comments (for listbox size):
To get the number of lines in a listbox you can use the .size() method.
Image:

Related

How to get only a corresponding entry box regardless of how many times I change radiobutton?

from tkinter import *
win=Tk()
var = StringVar()
l = Label(win, bg='white', width=15)
l.grid(row=17,column=1,padx=10, pady=10, sticky='w')
def print1_selection():
if var.get()=="Number":
lab1= Label(win, text="Enter a number").grid(row=4, column=0)
ent1=Entry(win).grid(row=4, column=1)
l.config(text='you have selected ' + var.get())
elif var.get()=="Alphabet":
lab21= Label(win, text="Enter an alphabet").grid(row=5, column=0)
ent21=Entry(win).grid(row=5, column=1)
l.config(text='you have selected ' + var.get())
lbl4=Label(win, text="Select One", bg="crimson", fg="white", font=("times new
roman",15,"bold")).grid(row=1, column=0, padx=10, pady=10, sticky='w')
r1 = Radiobutton(win, text='Number',variable=var, value='Number', command=print1_selection, width=22)
r1.grid(row=2,column=0,padx=10, pady=10)
r2 = Radiobutton(win, text='Alphabet', variable=var, value='Alphabet', command=print1_selection, width=22)
r2.grid(row=2,column=1,padx=10, pady=10)
win.mainloop()
In this code I want that when I select radiobutton number, only enter a number should appear and same for the other.
But the problem is that when I select number after selecting alphabet, it shows both. I need only the selected one and eliminate the other instantly.
This is how I would approach this issue:
from tkinter import Tk, StringVar, Label, Frame, Entry, Radiobutton
def print1_selection():
for widget in entry_frame.winfo_children():
widget.destroy()
value = var.get()
lbl.config(text='You have selected ' + value)
if value == "Number":
Label(entry_frame, text="Enter a number").grid(row=0, column=0)
Entry(entry_frame).grid(row=0, column=1)
elif value == "Alphabet":
Label(entry_frame, text="Enter an alphabet").grid(row=0, column=0)
Entry(entry_frame).grid(row=0, column=1)
win = Tk()
var = StringVar(value=0)
entry_frame = Frame(win)
entry_frame.grid(row=2, column=0, columnspan=2)
lbl = Label(win, bg='white', width=20)
lbl.grid(row=3, column=0, columnspan=2, padx=10, pady=10, sticky='w')
Label(win, text="Select One", bg="crimson", fg="white", font=("times new roman", 15, "bold")).grid(row=0, column=0, padx=10, pady=10, sticky='w')
Radiobutton(win, text='Number', variable=var, value='Number', command=print1_selection, width=22).grid(row=1, column=0, padx=10, pady=10)
Radiobutton(win, text='Alphabet', variable=var, value='Alphabet', command=print1_selection, width=22).grid(row=1, column=1, padx=10, pady=10)
win.mainloop()
As You can see if You don't plan on using the widgets instance anywhere You don't have to assign it to a variable. Also no need to configure label in both statements since that will be done anyways so just do it at the beginning, also rows start from 0 too. Frames help with organizing widgets. Also if You want neither of the radiobuttons selected set the variable to 0.

How to get paragraph data from Text widget in Tkinter for Python 3.x? How do I take paragraph input from the user and add the data to the CSV file?

Unable to extract data from Text widget in Tkinter, Python. However, it works fine for the Entry widgets that are in the code.
My purpose is to take paragraph input from the user and then add the data to a CSV file and subsequently generate a prescription using Photoshop.
Here is the code:
main.py
import abc
import csv, datetime
from tkinter import *
from tkinter import ttk
now=datetime.datetime.now().strftime("%d-%m-%Y")
def DataAdder2CSV():
global edate, eSNO, eage, egender, ename, ePID, econtact, ecomp, eallergy, ehistory, eR
a=edate.get()
b=eSNO.get()
c=eage.get()
d=egender.get()
e=ename.get()
f=ePID.get()
g=econtact.get()
data=[a,b,c,d,e,f,g,ecomp, eallergy, ehistory, eR]
print(data)
file=open("Patient_Data.csv","a", newline="")
writer=csv.writer(file, delimiter=",")
writer.writerow(data)
file.close()
def PrescGen():
pass
root=Tk()
root.config(bg="#add8e6")
megaF=LabelFrame(root,bg="#add8e6", borderwidth=0)
megaF.pack()
greet=Label(megaF,text="Prescription Maker", bg="#add8e6", fg="black", font="LucidaConsole 30").pack(pady=20)
dateF=Frame(megaF, padx=10, pady=10, bg="#c3dde6")
dateF.pack()
date=Label(dateF,text="Date:", bg="#c3dde6").grid(row=0,column=0)
edate=Entry(dateF, width=10)
edate.insert(0,now)
edate.grid(row=0, column=1)
mainL=Frame(root,borderwidth=0, padx=10, pady=10, bg="#c3dde6")
mainL.pack(pady=20, padx=40)
PDlf=Frame(mainL, padx=10, pady=13, bg="#c3dde6")
PDlf.grid(row=0, column=0, sticky=NW)
sno=Label(PDlf,text="Sno:", bg="#c3dde6").grid(row=0,column=0)
eSNO=Entry(PDlf, width=3)
eSNO.grid(row=0, column=1)
age=Label(PDlf,text="Age:", bg="#c3dde6").grid(row=1,column=0)
eage=Entry(PDlf, width=3)
eage.grid(row=1, column=1, pady=10)
gender=Label(PDlf,text="Gender:", bg="#c3dde6").grid(row=2,column=0)
egender=Entry(PDlf, width=3)
egender.grid(row=2, column=1)
name=Label(PDlf,text="Name:", bg="#c3dde6").grid(row=0,column=3)
ename=Entry(PDlf, width=15)
ename.grid(row=0, column=4)
Pid=Label(PDlf,text="PatientID:", bg="#c3dde6").grid(row=1,column=3)
ePID=Entry(PDlf, width=15)
ePID.grid(row=1, column=4, pady=10)
contact=Label(PDlf,text="Contact:", bg="#c3dde6").grid(row=2,column=3)
econtact=Entry(PDlf, width=15)
econtact.grid(row=2, column=4)
blank=Label(PDlf,text=" ", bg="#c3dde6").grid(row=1,column=2)
contentLF=LabelFrame(mainL, padx=10, pady=10, bg="#c3dde6", borderwidth=0)
contentLF.grid(row=0, column=2, sticky=SE)
#Never gonna give you up, never gonna let you down
blank=Label(PDlf,text=" ", bg="#c3dde6").grid(row=0,column=2)
complaint=Label(contentLF,text="Complaint:", bg="#c3dde6").grid(row=0,column=1)
ecomp=Text(contentLF, width=50, height=3).grid(row=0, column=2)
allergy=Label(contentLF,text="Allergy:", bg="#c3dde6").grid(row=1,column=1)
eallergy=Text(contentLF, width=50, height=2)
eallergy.grid(row=1, column=2, pady=10)
history=Label(contentLF,text="History:", bg="#c3dde6").grid(row=2,column=1)
ehistory=Text(contentLF, width=50, height=5)
ehistory.grid(row=2, column=2)
R=Label(contentLF,text="Diagnosis:", bg="#c3dde6").grid(row=3,column=1)
eR=Text(contentLF, width=50, height=12)
eR.grid(row=3, column=2, pady=10)
buttF=LabelFrame(mainL, bg="#c3dde6", borderwidth=0)
buttF.grid(row=4, column=2, sticky=E)
genPres=Button(buttF,text="Generate Prescription", bg="#ff80b9", command=PrescGen).grid(row=0, column=0, padx=10, sticky=E)
csvAdd=Button(buttF,text="Add to Database", bg="#49ff9a", command=DataAdder2CSV).grid(row=0, column=1,padx=10, sticky=E)
pic=Frame(mainL, padx=10, pady=15, bg="#c3dde6")
pic.grid(row=1, column=0, sticky=NW)
root.title("Prescription Maker")
root.state("zoomed")
root.mainloop()
Here is the output:
Here is the CSV file where I extracted all the data to:
How do I take paragraph input from the user and add the data to the CSV file (Tkinter)?

Using a Tkinter Widget from Lower Down in the Program

So I'm trying to make this tkinter GUI in which you can add or search for a customer (the customer is stored in a SQlite database). Problem is, I can't see a way of making the def for when the add customer button is pressed, for as far as I know, I would need to use the names of the variables from lower down in the program where the widgets are created. I could switch the order of the functions around, but then the widgets would be calling functions that have not already been created. Can someone help fix this probem? Thanks in advance.
Here's the code:
from tkinter import *
app = Tk()
app.minsize(400,420)
app.title("Gatecode Manager")
app.resizable(False, False)
def add():
first = createWindow().firstAdd.get()
last = createWindow().firstAdd.get()
phone = createWindow().numberAdd.get()
def createWindow():
Label(text="Welcome to Gatecode Manager", font=("bold", 17)).grid(row=0, columnspan=2, pady=30, sticky=N)
Label(text="Search for a Customer").grid(row=1, column=0, padx=30, pady=10)
Label(text="First Name:").grid(row=2, column=0, padx=30, pady=10)
firstSearch = Entry().grid(row=3, column=0, padx=30)
Label(text="Last Name:").grid(row=4, column=0, padx=30, pady=10)
lastSearch = Entry().grid(row=5, column=0, padx=30)
Label(text="Add Customer to System").grid(row=1, column=1, padx=30, pady=10)
Label(text="First Name:").grid(row=2, column=1, padx=30, pady=10)
firstAdd = Entry().grid(row=3, column=1, padx=30)
Label(text="Last Name:").grid(row=4, column=1, padx=30, pady=10)
lastAdd = Entry().grid(row=5, column=1, padx=30)
Label(text="Phone Number:").grid(row=6, column=1, padx=30, pady=10)
numberAdd = Entry().grid(row=7, column=1, padx=30)
add = Button(text="Add", command="null").grid(padx=30, pady=10, column=1)
app.mainloop()
createWindow()
I find it difficult to understand exactly how you want this program to function. Is the add button supposed to call the function add() which then calls createWindow()?
I'm guessing a little here but what about:
1) Don't create the GUI in a function. A function will keep the variables in the scope of the functioin.
2) Use StringVar() to access entries. Since you don't save the identity of your entries it becomes impossible to interrogate them.
Example:
from tkinter import *
app = Tk()
app.minsize(400,420)
app.title("Gatecode Manager")
app.resizable(False, False)
def add():
first = firstAddVar.get()
last = firstAddVar.get()
phone = numberAddVar.get()
Label(text="Welcome to Gatecode Manager", font=("bold", 17)).grid(row=0,
columnspan=2, pady=30, sticky=N)
Label(text="Search for a Customer").grid(row=1, column=0, padx=30, pady=10)
Label(text="First Name:").grid(row=2, column=0, padx=30, pady=10)
firstSearchVar = StringVar()
firstSearch = Entry(textvar=firstSearchVar).grid(row=3, column=0, padx=30)
Label(text="Last Name:").grid(row=4, column=0, padx=30, pady=10)
lastSearchVar = StringVar()
lastSearch = Entry(textvar=lastSearchVar).grid(row=5, column=0, padx=30)
Label(text="Add Customer to System").grid(row=1, column=1, padx=30, pady=10)
Label(text="First Name:").grid(row=2, column=1, padx=30, pady=10)
firstAddVar = StringVar()
firstAdd = Entry(textvariable=firstAddVar).grid(row=3, column=1, padx=30)
Label(text="Last Name:").grid(row=4, column=1, padx=30, pady=10)
lastAddVar = StringVar()
lastAdd = Entry(textvariable=lastAddVar).grid(row=5, column=1, padx=30)
Label(text="Phone Number:").grid(row=6, column=1, padx=30, pady=10)
numberAddVar = StringVar()
numberAdd = Entry(textvariable=numberAddVar).grid(row=7, column=1, padx=30)
add = Button(text="Add", command=add).grid(padx=30, pady=10, column=1)
app.mainloop()
When your GUIs become more complex you should study OOP.

tkinter callback method not defined global name not defined

I would like to know why the methods SavePVParms Close and ApplyGraph are not defined when I call them through the buttons. I know that if I put them inside __init__ they will work and my problem is solved but I don't understand why it wouldn't work the way it is. I tried to look for this question and found this Python Class: Global/Local variable name not defined and this Python says global name not defined but forgive me if I can't understand the answer there. Could you help me understand why?
Background: This class is a top level window that popups when I press a button in the root window (here i am just showing you guys the toplevel window directly). It is supposed to pass a dictionary of all the entries when I save it (not yet implemented). **Additionally (but not required to answer if you don't want to) is this an adequate style of coding in OOP? (less than 1 week learning tkinter and moving from precedural python to OOP python).
import Tkinter as tk
import ttk
class PVDialog(tk.Toplevel):
def SavePVParms(self):
print "saved"
self.destroy()
def Close(self):
self.destroy()
def ApplyGraph(self):
print 'applied'
def __init__(self, parent):
tk.Toplevel.__init__(self, parent)
print parent
self.title('PV Parameters Configuration')
self.geometry('800x480')
self.resizable(width=False, height=False)
self.columnconfigure(0, weight=1)
self.columnconfigure(1, weight=8)
self.rowconfigure(0, weight=1)
self.rowconfigure(1, weight=2)
# ====== FRAMES =======
lb1Frame = tk.LabelFrame(self, text='Electric Properties')
lb1Frame.grid(row=0, column=0, sticky='ewns', pady=(15, 2), padx=(30,10))
AdvFrame = tk.LabelFrame(self, text='Thermal Properties')
AdvFrame.grid(row=1, column=0, sticky='ewns', pady=5, padx=(30,10))
pcurveFrame = tk.LabelFrame(self, text='PV Curves')
pcurveFrame.grid(row=0, column=1, sticky='ewns', padx=(0,30), pady=(15,5),rowspan=2)
ctrlFrame = tk.Frame(self)
ctrlFrame.grid(row=2, column=0, columnspan=2, sticky='ens', padx=20, pady=(2, 20))
# ======== PARAMETER INPUT DATA FRAME ============= #
labelName = tk.Label(lb1Frame, text='Name', anchor='w')
labelCellType = tk.Label(lb1Frame, text='Cell Type', anchor='w')
labelPower = tk.Label(lb1Frame, text='Rated Power [W]', anchor='w')
labelOV = tk.Label(lb1Frame, text='Open Voltage [V]', anchor='w')
labelSCC = tk.Label(lb1Frame, text='Short-circuit Current [A]', anchor='w')
labelMPC = tk.Label(lb1Frame, text='Maximum Point Current [I]', anchor='w')
labelMPV = tk.Label(lb1Frame, text="Maximum point Voltage [V]", anchor='w')
labelSeries = tk.Label(lb1Frame, text='Cells in series', anchor='w')
labelPallel = tk.Label(lb1Frame, text='Cells in parallel', anchor='w')
labelNOCT = tk.Label(AdvFrame, text='NOCT', anchor='w')
labelThC = tk.Label(AdvFrame, text='Current T factor [%/K]', anchor='w')
labelThV = tk.Label(AdvFrame, text='Voltage T factor [%/K]', anchor='w')
labelThP = tk.Label(AdvFrame, text='Power T factor [%/K]', anchor='w')
labelName.grid(row=0, sticky='ew', padx=10, pady=2)
labelCellType.grid(row=1, sticky='ew', padx=10, pady=2)
labelPower.grid(row=2, sticky='ew', padx=10, pady=2)
labelOV.grid(row=3, sticky='ew', padx=10, pady=2)
labelSCC.grid(row=4, sticky='ew', padx=10, pady=2)
labelMPC.grid(row=5, sticky='ew', padx=10, pady=2)
labelMPV.grid(row=6, sticky='ew', padx=10, pady=2)
labelSeries.grid(row=7, sticky='ew', padx=10, pady=2)
labelPallel.grid(row=8, sticky='ew', padx=10, pady=2)
labelNOCT.grid(row=9 , sticky='ew', padx=10, pady=2)
labelThC.grid(row=10, sticky='ew', padx=10, pady=2)
labelThV.grid(row=11, sticky='ew', padx=10, pady=2)
labelThP.grid(row=12, sticky='ew', padx=10, pady=2)
entryName = ttk.Entry(lb1Frame, show='Default')
entryCellType = ttk.Combobox(lb1Frame)
entryPower = ttk.Entry(lb1Frame)
entryOV = ttk.Entry(lb1Frame)
entrySCC = ttk.Entry(lb1Frame)
entryMPC = ttk.Entry(lb1Frame)
entryMPV = ttk.Entry(lb1Frame)
entrySeries = ttk.Entry(lb1Frame)
entryPallel = ttk.Entry(lb1Frame)
entryNOCT = ttk.Entry(AdvFrame, width=23)
entryThC = ttk.Entry(AdvFrame, width=23)
entryThV = ttk.Entry(AdvFrame, width=23)
entryThP = ttk.Entry(AdvFrame, width=23)
entryName.grid(row=0, column=1, sticky='ew')
entryCellType.grid(row=1, column=1, sticky='ew')
entryPower.grid(row=2, column=1, sticky='ew')
entryOV.grid(row=3, column=1, sticky='ew')
entrySCC.grid(row=4, column=1, sticky='ew')
entryMPC.grid(row=5, column=1, sticky='ew')
entryMPV.grid(row=6, column=1, sticky='ew')
entrySeries.grid(row=7, column=1, sticky='ew')
entryPallel.grid(row=8, column=1, sticky='ew')
entryNOCT.grid(row=9, column=1, sticky='w', padx=(26,0))
entryThC.grid(row=10, column=1, sticky='w', padx=(26,0))
entryThV.grid(row=11, column=1, sticky='w', padx=(26,0))
entryThP.grid(row=12, column=1, sticky='w', padx=(26,0))
# ==== BUTTON COMANDS ======
saveBttn = ttk.Button(ctrlFrame, text='Save', command=SavePVParms)
closBttn = ttk.Button(ctrlFrame, text='Cancel', command=Close)
applyBttn = ttk.Button(ctrlFrame, text='Apply', command=ApplyGraph)
saveBttn.grid()
closBttn.grid(row=0, column=1, padx=10)
applyBttn.grid(row=0, column=2, padx=(0, 10))
# ======== RUNNIG THE CLIENT ============
root = tk.Tk()
dialog = PVDialog(root)
root.mainloop()
Update: As I was making about to submit this question, I remembered that __init__ is the first thing that is read and executed in the Class, so that's why the are defined if I put them in __init__. I still don't understand why python can't call them if they are outside __init__ doesn't python read the whole class? how can I call then self.method that is outside of __init__ then?. Thanks!
SavePVParms, Close, ApplyGraph methods are belong to your class, so you need to use self. to tell interpreter that, you want to use methods from your class.
saveBttn = ttk.Button(..., command=self.SavePVParms)
For coding standarts, you should check PEP 8 but as long as you are consistent with your style, it shouldn't matter that much.

How to add more than one feature into the same cell using the grid method, Python Tkinter

There might be question like this, but I can't find it.
I want to have more than one entry or label etc. in the same cell without them overlapping. I hope you know what I mean.
Any ideas?
Put as many items as you want in a frame, and then put the frame in the grid cell.
Example
import tkinter as tk
root = tk.Tk()
# some random widgets, for illustrative purposes
l0 = tk.Label(root, text="Cell 0,0", borderwidth=1, relief="solid")
l1 = tk.Label(root, text="Cell 0,1", borderwidth=1, relief="solid")
l2 = tk.Label(root, text="Cell 1,0", borderwidth=1, relief="solid")
l3 = tk.Label(root, text="Cell 1,1", borderwidth=1, relief="solid")
l4 = tk.Label(root, text="Cell 1,2", borderwidth=1, relief="solid")
# create a frame for one of the cells, and put
# a label and entry widget in it
f1 = tk.Frame(root, borderwidth=1, relief="solid")
l5 = tk.Label(f1, text="Cell 0,2")
e1 = tk.Entry(f1)
# put the label and entry in the frame:
l5.pack(side="top", fill="both", expand=True)
e1.pack(side="top", fill="x")
# put the widgets in the root
l0.grid(row=0, column=0, padx=2, pady=2, sticky="nsew")
l1.grid(row=0, column=1, padx=2, pady=2, sticky="nsew")
f1.grid(row=0, column=2, padx=2, pady=2, sticky="nsew")
l2.grid(row=1, column=0, padx=2, pady=2, sticky="nsew")
l3.grid(row=1, column=1, padx=2, pady=2, sticky="nsew")
l4.grid(row=1, column=2, padx=2, pady=2, sticky="nsew")
root.mainloop()

Categories