I am creating a form for session variables in python and (currently) store the inputs in separate text files. How could I use a single file to hold all session variables in lists (there will be more than 2 in the final form)? What would I need to change in my code to make this more efficient? Any help is greatly appreciated.
import tkinter
from tkinter import *
from tkinter import ttk
regionList = open('regions.txt','r')
optionList = open('options.txt','r')
class MainWindow(Frame):
def __init__(self,master = None):
Frame.__init__(self,master)
self.master = master
self.init_window()
self.grid()
self.create_widgets()
def create_widgets(self):
"""Create Window Layout"""
Boxfont = ('Lucida Grande', 12)
self.label1 = Label(self, font=Boxfont,
text="Regions").grid(row=2,column=0)
self.regcombo = ttk.Combobox(self, font = Boxfont, width = 20, textvariable = varRegions)
self.regcombo.bind("<Return>", self.regcombo_onEnter)
self.regcombo.bind('<<ComboboxSelected>>',self.regcombo_onEnter)
self.regcombo['values'] = regionList.readlines()
self.regcombo.grid(row=2, column=1,sticky = W)
self.label2 = Label(self, font=Boxfont, text="Options").grid(row=4,column=0)
self.optcombo = ttk.Combobox(self, font = Boxfont, width = 20, textvariable = varOptions)
self.optcombo.bind("<Return>", self.optcombo_onEnter)
self.optcombo.bind('<<ComboboxSelected>>',self.optcombo_onEnter)
self.optcombo['values'] = optionList.readlines()
self.optcombo.grid(row=4, column=1,sticky = W)
def init_window(self):
self.master.title("User Settings")
self.pack(fill=BOTH, expand=1)
def regcombo_onEnter(self,event):
varRegions.set(varRegions.get().lower())
mytext = varRegions.get()
vals = self.regcombo.cget('values')
self.regcombo.select_range(0,END)
print(mytext)
if not vals:
self.regcombo.configure(values = (mytext.strip,))
elif mytext not in vals:
with open('regions.txt','a') as f:
f.write('\n'+ mytext)
self.regcombo.configure(values = vals + (mytext,))
f.close
return 'break'
def optcombo_onEnter(self,event):
varOptions.set(varOptions.get().lower())
mytext = varOptions.get()
vals = self.optcombo.cget('values')
self.optcombo.select_range(0,END)
print(mytext)
if not vals:
self.optcombo.configure(values = (mytext.strip,))
elif mytext not in vals:
with open('options.txt','a') as f:
f.write('\n'+ mytext)
self.optcombo.configure(values = vals + (mytext,))
f.close
return 'break'
root = tkinter.Tk()
root.geometry("600x600")
varRegions = tkinter.StringVar(root, value='')
varOptions = tkinter.StringVar(root, value='')
app = MainWindow(root)
root.mainloop()
Related
from tkinter import Tk, Frame, Button, Label, Entry, ttk, StringVar, messagebox
import datetime
# Main Window
class WINDOW(Tk):
def __init__(self, master):
Tk.__init__(self, master)
self.master = master
frame1 = Frame1(self)
frame1.grid(row=0,column=0)
class Frame1(Frame):
def __init__(self, master):
Frame.__init__(self, master,height=master.winfo_screenheight(),
width=master.winfo_screenwidth())
self.master = master
var = StringVar()
label = Label(self, text="DATA", font=('calibre', 10, 'bold'))
entry = Entry(self, textvariable=var,width=10, font=('calibre', 10, 'normal'))
label.grid(row=0, column=1)
entry.grid(row=1, column=1)
# Log data sheet
NewTree = ttk.Treeview(self, height=23, columns=("Time","Time Diff", "DATA"))
NewTree['show'] = 'headings'
NewTree['columns'] = ("Time","Time Diff", "DATA")
NewTree.column("Time", width=180)
NewTree.column("Time Diff", width=150)
NewTree.column("DATA", width=150)
NewTree.heading("Time",text='TIME')
NewTree.heading("Time Diff",text='TimeDIfference')
NewTree.heading("DATA",text='DATA')
NewTree.grid(row=5, columnspan=4)
def insert_data():
if entry.get()=='':
messagebox.showerror("","Please insert data")
else:
NewTree.insert('', 'end',values=(datetime.datetime.now(),"",entry.get()))
entry.delete(0, 'end')
submit_button = Button(self, text="SUBMIT", command=insert_data)
submit_button.grid(row=3, column=4)
root = WINDOW(None)
root.geometry(f'{root.winfo_screenwidth()}x{root.winfo_screenheight()}')
root.title("ADD DATA")
root.mainloop()
Please refer the image:
there is time difference between submitting the 11 and 12, 12 and 24 and so on.
i need to calculate that time and want to add in time difference column.
it is like how much time it takes to add next data
I am not quite sure with what you are comparing the value, but my guess is the previous time.
you can use relativedelta to get the time difference.
In the beginning, assign both currenttime = previoustime = datetime.datetime.now(). Then when ever the user calls update again assign currenttime=datetime.datetime.now() find the time difference, display the data in the treeview and then update previoustime=currenttime
Here is an example.
from tkinter import ttk,Tk
import datetime
from dateutil.relativedelta import relativedelta
def diff(t_a, t_b):
t_diff = relativedelta(t_b, t_a)
return ' {h}h {m}m {s}s {ms}ms'.format(h=abs(t_diff.hours), m=abs(t_diff.minutes), s=abs(t_diff.seconds), ms=abs(round(t_diff.microseconds*0.001, 2)))
def update():
global currenttime, previoustime, timediff
currenttime= datetime.datetime.now()
timediff = diff(currenttime, previoustime)
curr = tree.insert('','end',values=(currenttime,timediff))
previoustime = currenttime
tree.selection_set(curr)
tree.see(curr)
root = Tk()
tree = ttk.Treeview(root, height = 20, columns = 2)
tree['show']='headings'
tree['columns'] = ('one','two')
tree.column('one', width = 250)
tree.column('two', width = 250)
tree.heading('one', text = 'TIME')
tree.heading('two', text = 'TIME Difference')
tree.grid(row = 0, column = 0)
currenttime= datetime.datetime.now()
previoustime= currenttime
timeBtn = ttk.Button(root, text='Update', command=update)
timeBtn.grid(row=1, column=0)
root.geometry("500x500")
root.mainloop()
Update after OP's request:
class Frame1(Frame):
def __init__(self, master):
...
NewTree.grid(row=5, columnspan=4)
self.currenttime = None
self.previoustime = None
self.timediff = None
def diff(t_a, t_b):
t_diff = relativedelta(t_b, t_a)
return ' {h}h {m}m {s}s {ms}ms'.format(h=abs(t_diff.hours), m=abs(t_diff.minutes), s=abs(t_diff.seconds), ms=abs(round(t_diff.microseconds*0.001, 2)))
def update_time():
if entry.get()=='':
messagebox.showerror("","Please insert data")
else:
self.currenttime= datetime.datetime.now()
if self.previoustime is None:
self.previoustime = self.currenttime
self.timediff = diff(self.currenttime, self.previoustime)
curr = NewTree.insert('','end',values=(datetime.datetime.now(), self.timediff ,entry.get()))
self.previoustime = self.currenttime
NewTree.selection_set(curr)
NewTree.see(curr)
submit_button = Button(self, text="SUBMIT", command=update_time)
...
I am losing my peanuts here. I am trying to clear two label values but i get an error
AttributeError: 'Label' object has no attribute 'delete'
basically if i were to click the calculate subtotal button then click the divide total button. I get my intended values. Now if I were to click on the clear values button i get an error. Literally shaking my head as I type this. Anyone care to explain why this is the case?
try:
import Tkinter as tk
except:
import tkinter as tk
class GetInterfaceValues():
def __init__(self):
self.root = tk.Tk()
self.totalValue = tk.StringVar()
self.root.geometry('500x200')
self.calculateButton = tk.Button(self.root,
text='Calculate Subtotal',
command=self.getSubtotals)
self.divideTotalButton = tk.Button(self.root,
text='Divide total',
command=self.divide)
self.textInputBox = tk.Text(self.root, relief=tk.RIDGE, height=1, width = 6, borderwidth=2)
self.firstLabel = tk.Label(self.root, text="This is the subtotal:")
self.secondLabel = tk.Label(self.root, text="This is the Divide Total:")
self.clearTotalButton = tk.Button(self.root, text='clear the values',command = self.clear)
self.firstLabel.pack(side="bottom")
self.secondLabel.pack(side="bottom")
self.textInputBox.pack()
self.calculateButton.pack()
self.divideTotalButton.pack()
self.clearTotalButton.pack()
self.root.mainloop()
def getTextInput(self):
result = self.textInputBox.get("1.0", "end")
return result
def getSubtotals(self):
userValue = int(self.getTextInput())
self.firstLabel["text"] = self.firstLabel["text"] + str(userValue * 5)
def divide(self):
userValue = int(self.getTextInput())
self.secondLabel["text"] = self.secondLabel["text"] + str(userValue / 10)
def clear(self):
self.firstLabel["text"] = self.firstLabel.delete("1.0","end")
app = GetInterfaceValues()
try:
import Tkinter as tk
except:
import tkinter as tk
class GetInterfaceValues():
def __init__(self):
self.root = tk.Tk()
self.totalValue = tk.StringVar()
self.root.geometry('500x200')
self.calculateButton = tk.Button(self.root,
text='Calculate Subtotal',
command=self.getSubtotals)
self.divideTotalButton = tk.Button(self.root,
text='Divide total',
command=self.divide)
self.textInputBox = tk.Text(self.root, relief=tk.RIDGE, height=1, width = 6, borderwidth=2)
self.firstLabelDefault = "This is the subtotal:"
self.secondLabelDefault = "This is the Divide Total:"
self.firstLabel = tk.Label(self.root, text=self.firstLabelDefault)
self.secondLabel = tk.Label(self.root, text=self.secondLabelDefault)
self.clearTotalButton = tk.Button(self.root, text='clear the values',command = self.clear)
self.firstLabel.pack(side="bottom")
self.secondLabel.pack(side="bottom")
self.textInputBox.pack()
self.calculateButton.pack()
self.divideTotalButton.pack()
self.clearTotalButton.pack()
self.root.mainloop()
def getTextInput(self):
result = self.textInputBox.get("1.0", "end")
return result
def getSubtotals(self):
userValue = int(self.getTextInput())
self.firstLabel["text"] = self.firstLabel["text"] + str(userValue * 5)
def divide(self):
userValue = int(self.getTextInput())
self.secondLabel["text"] = self.secondLabel["text"] + str(userValue / 10)
def clear(self):
self.firstLabel["text"] = self.firstLabelDefault
self.secondLabel["text"] = self.secondLabelDefault
self.textInputBox.delete("1.0", "end")
app = GetInterfaceValues()
You may have confused the methods of tkinter.Text and tkinter.Label. The method you called was tkinter.label.delete, which is not defined (does not exist), however it does exist for the tkinter.Text. Therefore, the only way to 'reset' would be to change the text attribute of the tkinter.Labels back to a 'default' string. It would perhaps be more appropriate to use another widget instead.
I have this reproducible code, and I cannot understand why when I set the width value to 100 in the self.display_tcm_18 variable, it still does not increase its width. This self.display_tcm_18 lies within the self.xf Frame, which I set to a width value of 300 (wide enough to host a Label widget of 100). I don't know what I am missing in the Logic of building this GUI with tkinter. Does anyone could give me some hint?
import Tkinter
from Tkinter import *
import tkFileDialog
from tkFileDialog import askopenfilename
from tkFileDialog import askdirectory
class Window(Frame):
def __init__(self, master = None):
self.master = master
path = "logo.gif"
self.image = Tkinter.PhotoImage(file=path)
self.f = Frame(master, width=300, height =70)
self.sf = Frame(master, width=300, height=70)
self.xf = Frame(self.f,width = 300, relief=GROOVE, borderwidth = 2)
self.file_name_18 = ''
self.var = IntVar()
def browse_button():
filename = askopenfilename(filetypes = (("GEOTIFF Files", "*.tif"),))
self.file_name = filename
self.display.config(text = filename)
print(self.file_name)
self.Logo = Label(self.master, image = self.image).pack(side=TOP, padx=5)
self.open_tcm_button = Button(self.xf, text = "Open..", command = browse_button).pack(side=LEFT, padx = 5, pady = 10)
self.display_tcm_18 = Label(self.xf, width = 100, bg = "white", textvariable = self.file_name_18, relief = SUNKEN, anchor = W)
self.display_tcm_18.pack(side=LEFT)
self.tcm18_label = Label(self.f, text = "Tree Cover Mask 2018 ").place(relx=0.06, rely=0.125,anchor=W)
self.xf.place(relx=0.01, rely=0.125, anchor=NW)
self.f.pack(side=TOP)
root = Tk()
root.geometry("600x400")
app = Window(root)
root.mainloop()
Trying to print the reviews array as a list to my label so it doesn't just come out in a single line.
import pandas as pd
import numpy as np
from tkinter import *
from tkinter.filedialog import askopenfilename
from tkinter.messagebox import showwarning, showinfo
movies = pd.read_csv('C:/Users/Admin/Python Programs/ml-latest-small/movies.csv')
ratings = pd.read_csv('C:/Users/Admin/Python Programs/ml-latest-small/ratings.csv')
ratings.drop(['timestamp'], axis=1, inplace= True)
class App(Frame):
def replace_name(x):
return movies[movies['movieId']==x].title.values[0]
ratings.movieId = ratings.movieId.map(replace_name)
M = ratings.pivot_table(index=['userId'], columns=['movieId'], values='rating')
def pearsons(s1, s2):
s1_c = s1 - s1.mean()
s2_c = s2 - s2.mean()
return np.sum(s1_c * s2_c) / np.sqrt(np.sum(s1_c ** 2) * np.sum(s2_c ** 2))
def get_recs(self):
movie_name = self.mn.get()
num_recs_str = self.nr.get()
num_recs = int(num_recs_str)
reviews = []
for title in App.M.columns:
if title==movie_name:
continue
cor = App.pearsons(App.M[movie_name], App.M[title])
if np.isnan(cor):
continue
else:
reviews.append((title, cor))
reviews.sort(key=lambda tup: tup[1], reverse=True)
for x in reviews:
self.label3.config(text= "\n" + str(reviews[:num_recs]))
#self.label3.config(text=reviews[:num_recs])
return reviews[:num_recs]
def __init__(self, master):
Frame.__init__(self, master)
self.filename = None
label1=Label(master, text="Movie: ").grid(row=0)
label2=Label(master, text="Recommendations: ").grid(row=1)
self.label3=Label(master, text = "", font = 'Purisa', fg='blue')
self.label3.grid(row = 3)
self.mn = Entry(master)
self.mn.grid(row = 0, column = 1)
#self.mn.delete(0, END)
self.mn.insert(0, "Enter Movie Name")
self.nr = Entry(master)
self.nr.grid(row = 1, column = 1)
#self.nr.delete(0, END)
self.nr.insert(0, "Enter Number of Recommendations")
button1 = Button(self, text="Start", command=self.get_recs)
button2 = Button(self, text="Exit", command=master.destroy)
button1.grid(row = 2, padx= 5)
button2.grid(row = 2, column = 1, padx = 5)
self.grid()
root = Tk()
root.title("Recommender")
root.geometry("500x500")
app = App(root)
root.mainloop()
Here is an image of the current output
I couldn't modify your code because I don't have the files you opened in your code. However, this is an idea to solve your question:
from tkinter import *
things_to_print=['something','one thing','another']
root=Tk()
for i in range(len(things_to_print)):
exec('Label%d=Label(root,text="%s")\nLabel%d.pack()' % (i,things_to_print[i],i))
root.mainloop()
I think you can solve your problem by substituting your reviews into my things_to_print and maybe some other slight modifications.
Python 3.4.3, Windows 10, Tkinter
I am attempting to create a combobox that allows for multiple selections from the dropdown. I have found similar work for listbox (Python Tkinter multiple selection Listbox), but cannot get it work with the combobox.
Is there a simple way to enable multiple selection from the dropdown of the combobox?
By design the ttk combobox doesn't support multiple selections. It is designed to allow you to pick one item from a list of choices.
If you need to be able to make multiple choices you can use a menubutton with an associated menu, and add checkbuttons or radiobuttons to the menu.
Here's an example:
import Tkinter as tk
class Example(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
menubutton = tk.Menubutton(self, text="Choose wisely",
indicatoron=True, borderwidth=1, relief="raised")
menu = tk.Menu(menubutton, tearoff=False)
menubutton.configure(menu=menu)
menubutton.pack(padx=10, pady=10)
self.choices = {}
for choice in ("Iron Man", "Superman", "Batman"):
self.choices[choice] = tk.IntVar(value=0)
menu.add_checkbutton(label=choice, variable=self.choices[choice],
onvalue=1, offvalue=0,
command=self.printValues)
def printValues(self):
for name, var in self.choices.items():
print "%s: %s" % (name, var.get())
if __name__ == "__main__":
root = tk.Tk()
Example(root).pack(fill="both", expand=True)
root.mainloop()
Try this maybe...
import tkinter as Tkinter
import tkinter.font as tkFont
import tkinter.ttk as ttk
class Picker(ttk.Frame):
def __init__(self, master=None,activebackground='#b1dcfb',values=[],entry_wid=None,activeforeground='black', selectbackground='#003eff', selectforeground='white', command=None, borderwidth=1, relief="solid"):
self._selected_item = None
self._values = values
self._entry_wid = entry_wid
self._sel_bg = selectbackground
self._sel_fg = selectforeground
self._act_bg = activebackground
self._act_fg = activeforeground
self._command = command
ttk.Frame.__init__(self, master, borderwidth=borderwidth, relief=relief)
self.bind("<FocusIn>", lambda event:self.event_generate('<<PickerFocusIn>>'))
self.bind("<FocusOut>", lambda event:self.event_generate('<<PickerFocusOut>>'))
self._font = tkFont.Font()
self.dict_checkbutton = {}
self.dict_checkbutton_var = {}
self.dict_intvar_item = {}
for index,item in enumerate(self._values):
self.dict_intvar_item[item] = Tkinter.IntVar()
self.dict_checkbutton[item] = ttk.Checkbutton(self, text = item, variable=self.dict_intvar_item[item],command=lambda ITEM = item:self._command(ITEM))
self.dict_checkbutton[item].grid(row=index, column=0, sticky=Tkinter.NSEW)
self.dict_intvar_item[item].set(0)
class Combopicker(ttk.Entry, Picker):
def __init__(self, master, values= [] ,entryvar=None, entrywidth=None, entrystyle=None, onselect=None,activebackground='#b1dcfb', activeforeground='black', selectbackground='#003eff', selectforeground='white', borderwidth=1, relief="solid"):
if entryvar is not None:
self.entry_var = entryvar
else:
self.entry_var = Tkinter.StringVar()
entry_config = {}
if entrywidth is not None:
entry_config["width"] = entrywidth
if entrystyle is not None:
entry_config["style"] = entrystyle
ttk.Entry.__init__(self, master, textvariable=self.entry_var, **entry_config, state = "readonly")
self._is_menuoptions_visible = False
self.picker_frame = Picker(self.winfo_toplevel(), values=values,entry_wid = self.entry_var,activebackground=activebackground, activeforeground=activeforeground, selectbackground=selectbackground, selectforeground=selectforeground, command=self._on_selected_check)
self.bind_all("<1>", self._on_click, "+")
self.bind("<Escape>", lambda event: self.hide_picker())
#property
def current_value(self):
try:
value = self.entry_var.get()
return value
except ValueError:
return None
#current_value.setter
def current_value(self, INDEX):
self.entry_var.set(values.index(INDEX))
def _on_selected_check(self, SELECTED):
value = []
if self.entry_var.get() != "" and self.entry_var.get() != None:
temp_value = self.entry_var.get()
value = temp_value.split(",")
if str(SELECTED) in value:
value.remove(str(SELECTED))
else:
value.append(str(SELECTED))
value.sort()
temp_value = ""
for index,item in enumerate(value):
if item!= "":
if index != 0:
temp_value += ","
temp_value += str(item)
self.entry_var.set(temp_value)
def _on_click(self, event):
str_widget = str(event.widget)
if str_widget == str(self):
if not self._is_menuoptions_visible:
self.show_picker()
else:
if not str_widget.startswith(str(self.picker_frame)) and self._is_menuoptions_visible:
self.hide_picker()
def show_picker(self):
if not self._is_menuoptions_visible:
self.picker_frame.place(in_=self, relx=0, rely=1, relwidth=1 )
self.picker_frame.lift()
self._is_menuoptions_visible = True
def hide_picker(self):
if self._is_menuoptions_visible:
self.picker_frame.place_forget()
self._is_menuoptions_visible = False
if __name__ == "__main__":
import sys
try:
from Tkinter import Tk, Frame, Label
except ImportError:
from tkinter import Tk, Frame, Label
root = Tk()
root.geometry("500x600")
main =Frame(root, pady =15, padx=15)
main.pack(expand=True, fill="both")
Label(main, justify="left", text=__doc__).pack(anchor="w", pady=(0,15))
COMBOPICKER1 = Combopicker(main, values = [1, 2, 3, 4])
COMBOPICKER1.pack(anchor="w")
if 'win' not in sys.platform:
style = ttk.Style()
style.theme_use('clam')
root.mainloop()