Python GUI interface not opening widget? - python

I'm trying to create a widget that's like a to do list: but my program doesn't seem to run for some reason. It doesn't give an error, but it doesn't work..?
would anyone know what's wrong with my code?
from Tkinter import *
import tkFont
class App:
def getTasks(self):
return self.todo
def getCompleted(self):
return self.done
def __init__(self, master):
self.todo = todo.todoList()
self.master = master
self.frame - Frame(master)
self.frame.grid()
self.saveButton = Button(self.frame, text="Save", command=self.save)
self.saveButton.grid()
self.restoreButton = Button(self.frame, text="Restore", command=self.res)
self.restoreButton.grid(row=0, column=1)
self.addButton = Button(self.frame, text="Add", command = self.add)
self.addButton.grid(row=0, column=2)
self.doneButton = Button(self.frame, text = "Done", command = self.done)
self.doneButton.grid(row=0, column=3)
self.button = Button(self.frame, text="QUIT", command=self.quit)
self.button.grid(row=0, column=4)
label = Label(self.frame, text="New Task: ")
label.grid()
self.entry = Entry(self.frame)
self.entry.grid(row=0, column=4)
frame1 = LabelFrame(self.frame, text="Tasks")
frame1.grid(columnspam = 5)
self.tasks = Listbox(frame1)
self.task.grid()
frame2=LabelFrame(self.frame, text="Completed")
frame2.grid(columnspan=5)
self.completed= Listbox(frame2)
self.completed.grid()
def save(self):
self.todo.saveList("tasks.txt")
def restore(self):
self.todo.restoreList("tasks.txt")
items = self.todo.getTasks()
self.tasks.delete(0, END)
for item in items:
self.tasks.insert(END, item)
items = self.todo.getCompleted()
self.completed.delete(0,END)
for item in items:
self.completed.insert(END,item)
def add(self):
task = self.entry.get()
self.todo.addTask(task)
self.tasks.insert(END,task)
def done(self):
selection = self.tasks.curselection()
if len(selection) == 0:
return
task = self.tasks.get(selection[0])
self.todo.completeTask(task)
self.tasks.delete(selection[0])
self.completed.insert(END,task)
would anyone know what the error is?

You need to actually make an instance of App and an instance of Tkinter.Tk:
...
if __name__ == '__main__':
root = Tk()
app = App(root)
root.mainloop()
Note, I haven't tried the rest of your code, so I don't know if it will work, but this should at least start giving you errors to work with.

Related

I can not figure out why my app menu is still freezing after I have used threading on tkinter

When I run the app, the run button is still responsive for a few seconds before the entire app freezes but the thread continues running. Where is the problem in my code and how do fix it?
import threading
from tkinter import *
from tkinter import ttk
class SubExtractor:
def __init__(self, root):
self.root = root
self._create_layout()
def _create_layout(self):
self.root.title("Sub Extractor")
self._menu_bar()
self.main_frame = ttk.Frame(self.root, padding=(5, 5, 5, 15))
self._video_frame()
self._progress_frame()
self._output_frame()
self.main_frame.grid()
def _menu_bar(self):
self.root.option_add('*tearOff', FALSE)
menubar = Menu(self.root)
self.root.config(menu=menubar)
menu_file = Menu(menubar)
menu_settings = Menu(menubar)
menubar.add_cascade(menu=menu_file, label="File")
menubar.add_cascade(menu=menu_settings, label="Settings")
menu_file.add_command(label="Open", command=self._open_file)
menu_file.add_command(label="Close", command=self._close_window)
menu_settings.add_command(label="Language", command=self._language_settings)
menu_settings.add_command(label="Extraction", command=self._extraction_settings)
def _video_frame(self):
video_frame = ttk.Frame(self.main_frame, borderwidth=2, relief="ridge", width=1000, height=600)
video_frame.grid()
def _progress_frame(self):
progress_frame = ttk.Frame(self.main_frame)
progress_frame.grid(row=1, sticky="W")
self.run_button = ttk.Button(progress_frame, text="Run", command=self._run)
self.run_button.grid(pady=10, padx=30)
self.progress_bar = ttk.Progressbar(progress_frame, orient=HORIZONTAL, length=800, mode='determinate')
self.progress_bar.grid(column=2, row=0)
def _output_frame(self):
output_frame = ttk.Frame(self.main_frame)
output_frame.grid(row=2)
self.text_output_widget = Text(output_frame, width=97, height=12, state="disabled")
self.text_output_widget.grid()
output_scroll = ttk.Scrollbar(output_frame, orient=VERTICAL, command=self.text_output_widget.yview)
output_scroll.grid(column=1, row=0, sticky="N,S")
self.text_output_widget.configure(yscrollcommand=output_scroll.set)
def _close_window(self):
self._stop_run()
self.root.quit()
def _stop_run(self):
self.interrupt = True
self.run_button.configure(text="Run", command=self._run)
def _text_to_output(self, text):
self.text_output_widget.configure(state="normal")
self.text_output_widget.insert("end", f"{text}\n")
self.text_output_widget.see("end")
self.text_output_widget.configure(state="disabled")
def long_running_method(self):
num = 100000
self.progress_bar.configure(maximum=num)
for i in range(0, num):
if self.interrupt:
break
self._text_to_output(f"Line {i} of {num}")
self.progress_bar['value'] += 1
self._stop_run()
def _run(self):
self.interrupt = False
self.run_button.configure(text='Stop', command=self._stop_run)
threading.Thread(target=self.long_running_method).start()
rt = Tk()
SubtitleExtractorGUI(rt)
rt.mainloop()
What I expect to accomplish is for the loop to insert texts in the text widget while I can still freely move and use other buttons on the app.

Python Tkinter - Unable to obtain entry value after widgets in window are destroyed

I'm writing a program using Tkinter that prompts the user to enter their name as a value, then displays it after the contents of the window are cleared. However, I'm unable to retrieve this value. I'd like to know how I could fix this.
Below is my code:
import tkinter
class Window:
def __init__(self):
self.main_window = tkinter.Tk()
self.main_window.title("Window Title")
self.main_window.geometry("300x100")
self.frame = tkinter.Frame(self.main_window)
self.label1 = tkinter.Label(self.frame,
text="Enter name:")
self.label1.pack(pady=5)
self.name_entry = tkinter.Entry(self.frame,
width=20)
self.name_entry.pack(pady=5)
self.name = tkinter.StringVar()
self.name_label = tkinter.Label(self.frame,
textvariable=self.name)
def frame2():
for widget in self.frame.winfo_children():
widget.destroy()
self.frame.pack_forget()
self.frame2.pack(pady=20)
self.name_enter = tkinter.Button(self.frame,
text="Confirm",
command=lambda:
[self.getname(),frame2()])
self.name_enter.pack(pady=5)
self.frame.pack()
self.frame2 = tkinter.Frame(self.main_window)
self.label2 = tkinter.Label(self.frame2,
text="Your name is " +
str(self.name.get()) +
".")
self.label2.pack(pady=5)
tkinter.mainloop()
def getname(self):
nameget = str(self.name_entry.get())
self.name.set(nameget)
if __name__ == '__main__':
window = Window()
Using the suggestion from jasonharper I edit the code for a possible fix. I added another label above your label that takes in the StringVar() and then changed the label parameters to textvariable=self.name which gets the input.
import tkinter
class Window:
def __init__(self):
self.main_window = tkinter.Tk()
self.main_window.title("Window Title")
self.main_window.geometry("300x100")
self.frame = tkinter.Frame(self.main_window)
self.label1 = tkinter.Label(self.frame,
text="Enter name:")
self.label1.pack(pady=5)
self.name_entry = tkinter.Entry(self.frame,
width=20)
self.name_entry.pack(pady=5)
self.name = tkinter.StringVar()
self.name_label = tkinter.Label(self.frame,
textvariable=self.name)
def frame2():
for widget in self.frame.winfo_children():
widget.destroy()
self.frame.pack_forget()
self.frame2.pack(pady=20)
self.name_enter = tkinter.Button(self.frame,
text="Confirm",
command=lambda:
[self.getname(),frame2()])
self.name_enter.pack(pady=5)
self.frame.pack()
self.frame2 = tkinter.Frame(self.main_window)
self.labelx = tkinter.Label(self.frame2, text="Your name is")
self.labelx.pack()
self.label2 = tkinter.Label(self.frame2, textvariable=self.name)
self.label2.pack(pady=5)
tkinter.mainloop()
def getname(self):
nameget = str(self.name_entry.get())
self.name.set(nameget)
if __name__ == '__main__':
window = Window()

How can I bind a combobox to a Radiobutton

I have some Radiobuttons. Depending of what Radio button was selected I want to have different Combobox values. I don't know how I can solve the problem. In a further step I want to create further comboboxes which are dependend on the value of the first.
The following code creates the list of user, but it does not show up in the combobox.
For me it is difficult to understand where the right position of functions is, and if I need a lambda function nor a binding.
import tkinter as tk
from tkinter import ttk
import pandas as pd
import os
global version
global df_MA
df_MA = []
class Window(tk.Toplevel):
def __init__(self, parent):
super().__init__(parent)
self.geometry('300x100')
self.title('Toplevel Window')
self.btn = ttk.Button(self, text='Close',command=self.destroy).pack(expand=True)
class App(tk.Tk):
def __init__(self,*args, **kwargs):
super().__init__()
# def load_input_values(self):
def set_department(department):
if department == "produktion":
working_df_complete = path_input_produktion
if department == "service":
working_df_complete = path_input_service
working_df_complete = pd.read_excel(working_df_complete)
working_df_complete = pd.DataFrame(working_df_complete)
'''set worker df'''
df_MA = working_df_complete.loc[:,'MA']
df_MA = list(df_MA.values.tolist())
def select_working_step():
return
'''Define Variable Bereich aofter clicking of radio button '''
'''SEEMS TO ME UNECCESSARY COMPLICATED, but I dont't know how to do it properly. I am no progammer'''
border = 10
spacey = 10
'''paths for input file'''
path_input_produktion = os.path.abspath('input_data\werte_comboboxen_produktion.xlsx')
path_input_service = os.path.abspath('input_data\werte_comboboxen_service.xlsx')
self.geometry('500x600')
'''Variablen for department'''
department = tk.StringVar()
department.set(" ")
'''place Frame department'''
self.rb_frame_abteilung = tk.Frame(self)
'''Radiobuttons for department'''
rb_abteilung_produktion = tk.Radiobutton(self.rb_frame_abteilung, text="Produktion", variable= department,
value="produktion", command= lambda: set_department(department.get()))
rb_abteilung_service = tk.Radiobutton(self.rb_frame_abteilung, text="Service", variable= department,
value="service", command= lambda: set_department(department.get()) )
rb_abteilung_produktion.pack(side="left", fill=None, expand=False, padx=10)
rb_abteilung_service.pack(side="left", fill=None, expand=False, padx =10)
self.rb_frame_abteilung.grid(row=5, column=1, sticky="nw", columnspan=99)
self.label_user = ttk.Label(self, text='user').grid(row=15,
column=15, pady=spacey,padx=border, sticky='w')
self.combobox_user = ttk.Combobox(self, width = 10, value= df_MA)
self.combobox_user.bind("<<ComboboxSelected>>", select_working_step)
self.combobox_user.grid(row=15, column=20, pady=spacey, sticky='w')
if __name__ == "__main__":
app = App()
app.mainloop()
ยดยดยด
I rewrote everything using indexes and removing global variables...
#!/usr/bin/python3
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
class App(tk.Tk):
"""Application start here"""
def __init__(self):
super().__init__()
self.protocol("WM_DELETE_WINDOW", self.on_close)
self.title("Simple App")
self.option = tk.IntVar()
self.departments = ('Produktion','Service')
self.df_MA_1 = ['Peter','Hans','Alfred']
self.df_MA_2 = ['Otto','Friedrich','Tanja']
self.init_ui()
self.on_reset()
def init_ui(self):
w = ttk.Frame(self, padding=8)
r = 0
c = 1
ttk.Label(w, text="Combobox:").grid(row=r, sticky=tk.W)
self.cbCombo = ttk.Combobox(w, values="")
self.cbCombo.grid(row=r, column=c, padx=5, pady=5)
r += 1
ttk.Label(w, text="Radiobutton:").grid(row=r, sticky=tk.W)
for index, text in enumerate(self.departments):
ttk.Radiobutton(w,
text=text,
variable=self.option,
value=index,
command= self.set_combo_values).grid(row=r,
column=c,
sticky=tk.W,
padx=5, pady=5)
r +=1
r = 0
c = 2
b = ttk.LabelFrame(self, text="", relief=tk.GROOVE, padding=5)
bts = [("Reset", 0, self.on_reset, "<Alt-r>"),
("Close", 0, self.on_close, "<Alt-c>")]
for btn in bts:
ttk.Button(b, text=btn[0], underline=btn[1], command = btn[2]).grid(row=r,
column=c,
sticky=tk.N+tk.W+tk.E,
padx=5, pady=5)
self.bind(btn[3], btn[2])
r += 1
b.grid(row=0, column=1, sticky=tk.N+tk.W+tk.S+tk.E)
w.grid(row=0, column=0, sticky=tk.N+tk.W+tk.S+tk.E)
def set_combo_values(self):
print("you have selected {0} radio option".format(self.option.get()))
self.cbCombo.set("")
if self.option.get() == 0:
self.cbCombo["values"] = self.df_MA_1
else:
self.cbCombo["values"] = self.df_MA_2
def on_reset(self, evt=None):
self.cbCombo.set("")
self.option.set(0)
self.set_combo_values()
def on_close(self,evt=None):
"""Close all"""
if messagebox.askokcancel(self.title(), "Do you want to quit?", parent=self):
self.destroy()
def main():
app = App()
app.mainloop()
if __name__ == '__main__':
main()

Trying to enable a button using an entry widget

I have two sets of code. One works the other doesn't and I don't know why. In the first set of code I am storing my application into a class and referencing everything there. In the second one I am storing everything in the main function and using it there.
The main details of the problem are here:
I am using a entry widget and a button widget which is initially disabled. I want the button's state to be normal when there is text in the entry widgets text field.
I have searched online for many answers to the first set of code but the closest I have gotten is the second set of code. First I tried to integrate it into my code however that did not work. So what I did was take the code and strip it to the bare minimum and put everything into the main function.
The main difference between the two is one is in a class and the other is in the main function.
import tkinter as tk
class Aplication(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self,master)
self.grid()
self.button_clicks = 0
something = tk.StringVar()
button3 = tk.Button(self, text="Print entry", padx = 10, height = 2, bg = "blue", state = "disabled")
entry = tk.Entry(self, textvariable = something)
something.trace('w', self.ActivateButton) #need this to always be exsecuted
entry.grid(column = 2, row = 1)
button3["command"] = lambda: self.PrintEntry(entry.get())
button3.grid(padx = 10, pady = 10)
def PrintEntry (self, entry):
print(entry)
def ActivateButton(self, *_):
if something.get():
button3['state'] = "normal"
else:
button3['state'] = "disabled"
if __name__ == '__main__':
top= tk.Tk()
top.title("Simple Button")
top.geometry("500x300")
app = Aplication(top)
top.mainloop()
def PrintEntry (entry):
print(entry)
def ActivateButton(*_):
if entry.get():
button3['state'] = "normal"
else:
button3['state'] = "disabled"
if __name__ == '__main__':
top= tk.Tk()
top.title("Simple Button")
top.geometry("500x300")
something = tk.StringVar()
button3 = tk.Button(top, text="Print entry", padx = 10, height = 2, bg = "blue", state = "disabled")
entry = tk.Entry(top, textvariable = something, bd = 2)
something.trace('w', ActivateButton)
entry.grid(column = 2, row = 3)
button3["command"] = lambda: PrintEntry(entry.get())
button3.grid(row = 3, column = 1, padx = 10, pady = 10)
top.mainloop()
There are no error messages; however you will find in the first set, the button's state never gets set to normal. Is there a way to do this for the first one? What is the difference between the two that makes it to where I can't enable the button in the first one if it is impossible?
You have to use self. to create class variables so they will be accesible in all methods in class. Currently you create local variable something in __init__ and it is deleted when Python ends __init__ - so finally it removes something.trace() and it doesn't check value in entry.
In this code I use self.something and self.button3 and it works correctly.
import tkinter as tk
class Aplication(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self,master)
self.grid()
self.button_clicks = 0
self.something = tk.StringVar()
self.button3 = tk.Button(self, text="Print entry", padx = 10, height = 2, bg = "blue", state = "disabled")
entry = tk.Entry(self, textvariable = self.something)
self.something.trace('w', self.ActivateButton) #need this to always be exsecuted
entry.grid(column = 2, row = 1)
self.button3["command"] = lambda: self.PrintEntry(entry.get())
self.button3.grid(padx = 10, pady = 10)
def PrintEntry (self, entry):
print(entry)
def ActivateButton(self, *_):
if self.something.get():
self.button3['state'] = "normal"
else:
self.button3['state'] = "disabled"
if __name__ == '__main__':
top= tk.Tk()
top.title("Simple Button")
top.geometry("500x300")
app = Aplication(top)
top.mainloop()
EDIT: the same little different - without lambda. I use self.entry (as class variable) directly in print_entry.
import tkinter as tk
class Aplication(tk.Frame):
def __init__(self, master):
super().__init__(master)#tk.Frame.__init__(self,master)
self.grid()
self.button_clicks = 0
self.something = tk.StringVar()
self.entry = tk.Entry(self, textvariable=self.something)
self.entry.grid(column=2, row=1)
self.button3 = tk.Button(self, command=self.print_entry, text="Print entry", padx=10, height=2, bg="blue", state="disabled")
self.button3.grid(padx=10, pady=10)
self.something.trace('w', self.activate_button) #need this to always be exsecuted
def print_entry(self):
print(self.entry.get())
def activate_button(self, *_):
if self.something.get():
self.button3['state'] = "normal"
else:
self.button3['state'] = "disabled"
if __name__ == '__main__':
top= tk.Tk()
top.title("Simple Button")
top.geometry("500x300")
app = Aplication(top)
top.mainloop()
As pointed out by #furas you need to prefix your variables in the constructor method def __init__ with self. so you can access attributes and methods since it literally represents an instance of the class. This link explains it in more detail https://stackoverflow.com/a/2709832/7585554.
import tkinter as tk
class Application(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master)
self.grid()
self.button_clicks = 0
self.something = tk.StringVar()
self.button3 = tk.Button(self, text="Print entry", padx=10, height=2, bg="blue", state="disabled")
self.entry = tk.Entry(self, textvariable=self.something)
self.something.trace('w', self.ActivateButton)
self.entry.grid(column=2, row=1)
self.button3["command"] = lambda: self.PrintEntry()
self.button3.grid(padx=10, pady=10)
def PrintEntry (self):
print(self.entry.get())
def ActivateButton(self, *_):
if self.something.get():
self.button3['state'] = "normal"
else:
self.button3['state'] = "disabled"
if __name__ == '__main__':
top = tk.Tk()
top.title("Simple Button")
top.geometry("500x300")
app = Application(top)
top.mainloop()

Display selected options from checkbox to a new list in next tkinter window

I want to display the selected options from my checkbox list in a new list in a new Tkinter window and when browsing it comes back to the main screen
(using Python 3.5 with Ubuntu 16.04).
import tkinter as tk
import tkMessageBox
lista=['jpeg','jfit','tiff','gif','png','bmp']
class PopUp(tk.Toplevel):
def __init__(self, number=10):
tk.Toplevel.__init__(self)
self.global_state = tk.BooleanVar()
cb = tk.Checkbutton(self, text="select/deselect all", variable=self.global_state, command=self.select_clear_states)
cb.grid(row=0, column=0, padx=5, pady=1)
self.states = []
for n in range(len(lista)):
var = tk.BooleanVar()
cb = tk.Checkbutton(self, text=str(lista[n]), variable=var)
cb.grid(row=n+1, column=0, padx=5, pady=1)
self.states.append(var)
def select_clear_states(self):
state = self.global_state.get()
for x in self.states:
x.set(state)
def popup(num):
win = PopUp(num)
root = tk.Tk()
b = tk.Button(root, text="5 checkboxes", command=lambda:popup(5))
b.pack()
root.mainloop()
Assuming I understand you correctly, if you wanted to do this with Checkbuttons you could do something like the below:
from tkinter import *
class App:
def __init__(self, root):
self.root = root
self.top = Toplevel(root)
self.frame = Frame(self.top)
self.frame.pack()
self.check = []
self.var = []
for i in range(10):
self.var.append(IntVar())
self.var[i].trace("w", self.callback)
self.check.append(Checkbutton(self.root, text="Option "+str(i), variable=self.var[i]))
self.check[i].pack()
def callback(self, *args):
self.frame.destroy()
self.frame = Frame(self.top)
self.frame.pack()
for i, c in zip(self.check, self.var):
if c.get() == 1:
Label(self.frame, text=i.cget("text")).pack()
root = Tk()
App(root)
root.mainloop()
Alternatively you might find that a Listbox accomplishes what you want and (IMO) looks a lot cleaner and more user friendly:
from tkinter import *
class App:
def __init__(self, root):
self.root = root
self.listbox = Listbox(self.root, selectmode=MULTIPLE)
self.listbox.pack()
for i in range(10):
self.listbox.insert(END, "Option "+str(i))
root = Tk()
App(root)
root.mainloop()
Using the Listbox you probably wouldn't even need two windows as the selection quite clearly shows which options are selected by highlighting them.

Categories

Resources