Here is my code so far for a GUI I am making in tkinter, I have attempted to change the background colour of the window but it doesn't seem to work.
from tkinter import *
from tkinter.font import Font
class Window(Frame):
def __init__(self, master = None):
Frame.__init__(self, master)
self.master = master
self.init_window()
def init_window(self):
self.master.title("Main Menu")
self.pack(fill=BOTH, expand=1)
videoButton = Button(self, text="Video Mode", font=helv18, bg="white", height=1, width=18)
videoButton.place(relx=0.5, rely=0.35, anchor=CENTER)
timelapseButton = Button(self, text="Time-lapse Mode", font=helv18, bg="white", height=1, width=18)
timelapseButton.place(relx=0.5, rely=0.6, anchor=CENTER)
root = Tk()
helv18 = Font(family='Helvetica', size=18, weight='bold')
root.config(bg='black')
root.geometry("480x320")
root.resizable(0, 0)
app = Window(root)
root.mainloop()
Related
In my code, I have to list boxes that are bound to 2 different functions.
here's the code:
from tkinter import *
import string
class App:
def change_dropdown(self, *args):
print(self.Lb1.curselection())
self.Lb2.insert(self.count, self.choices[self.Lb1.curselection()[0]])
self.count+=1
def delete_dropdown_selected(self, *args):
print(self.Lb2.curselection())
def __init__(self, master):
self.count = 0
self.left = Frame(master)
self.left.config()
self.left.pack(side=LEFT)
self.choices = []
self.yscroll = Scrollbar(master, orient=VERTICAL)
self.Lb1 = Listbox(self.left, selectmode=SINGLE, yscrollcommand=self.yscroll.set, font=50, bd=2)
self.Lb2 = Listbox(self.left, selectmode=SINGLE, bd=2)
for j in range(2):
for i in range(26):
self.Lb1.insert(i,string.ascii_lowercase[i])
self.choices.append(string.ascii_letters[i])
self.Lb1.config(width=50, height=30)
self.Lb1.pack(side=TOP, fill=BOTH, expand=1)
self.Lb2.config(font=30, width=50, height=10)
self.Lb2.pack(side=BOTTOM, fill=BOTH, expand=1, pady=10)
self.Lb2.bind('<<ListboxSelect>>', self.delete_dropdown_selected)
self.Lb1.bind('<<ListboxSelect>>', self.change_dropdown)
self.yscroll.pack(side=LEFT, fill=Y)
self.yscroll.config(command=self.Lb1.yview)
root = Tk()
root.resizable(width=False, height=False)
app = App(root)
root.mainloop()
The problem is that when I click an item in Lb2, it goes to change_dropdown() instead of delete_dropdown_selected(). I don't understand why because I specify it here:
self.Lb2.bind('<<ListboxSelect>>', self.delete_dropdown_selected)
use exportselection=0 option in the Tkinter Listbox.
self.Lb1 = Listbox(self.left, selectmode=SINGLE, yscrollcommand=self.yscroll.set, font=50, bd=2, exportselection=0)
self.Lb2 = Listbox(self.left, selectmode=SINGLE, bd=2, exportselection=0)
How to keep selections highlighted in a tkinter Listbox?
I am trying to create an application that will allow you to put in multiple websites and check if they are down. When I run the code the window opens and everything works fine until i click search. Then i get the following error
AttributeError: 'Window' object has no attribute 'text_entry'
Any help would be greatly appreciated
#Import everything from tkinter
from tkinter import *
import urllib.request
#Main window
class Window(Frame):
#Master Widget
def __init__(self, master = None):
Frame.__init__(self, master)
self.master = master
self.init_window()
#Creation of init_window
def init_window(self):
# changing the title of our master widget
self.master.title("GUI")
# allowing the widget to take the full space of the root window
self.pack(fill=BOTH, expand=1)
#Menu
menu = Menu(self.master)
self.master.config(menu=menu)
#File Menu option
file = Menu(menu)
file.add_command(label="Exit", command=self.client_exit)
menu.add_cascade(label="File", menu=file)
#Text Box
text_entry = Entry(self, width=20, bg="white")
text_entry.place(x=0, y=0)
#Submit button
searchButton = Button(self, text='SUBMIT', width=6,
command=self.search)
searchButton.place(x=200, y=30)
#Output Box
output = Text(self, width=20, height=1, bg="white")
output.place(x=0, y=50)
def search(self):
entered_text = self.text_entry.get()
output.delete(0.0, END)
definition=(int(urllib.request.urlopen(entered_text).getcode()))
output.insert(END, definition)
root = Tk()
#Size of the window
root.geometry("400x300")
#Window instance
app = Window(root)
#Show and mainloop
root.mainloop()
You haven't put text_entry on the self object and you can't access it in search function.
#Text Box
self.text_entry = Entry(self, width=20, bg="white")
self.text_entry.place(x=0, y=0)
I would like to update a label once I press one of the buttons.
Here is my code - I added a label (caled label1), now I have two issues:
It presents some gibberish
How do I update the label with text right when the user is pressing the Browse button?
from tkinter import *
import threading
class Window(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
self.init_window()
def init_window(self):
self.var = IntVar()
self.master.title("GUI")
self.pack(fill=BOTH, expand=1)
quitButton = Button(self, text="Exit", command=self.client_exit)
startButton = Button(self, text="Browse", command=self.start_Button)
label1 = Label(self, text=self.lable_1)
quitButton.grid(row=0, column=0)
startButton.grid(row=0, column=2)
label1.grid(row=1, column=0)
def client_exit(self):
exit()
def lable_1(self):
print('starting')
def start_Button(self):
def f():
print('Program is starting')
t = threading.Thread(target=f)
t.start()
root = Tk()
root.geometry("250x50")
app = Window(root)
root.title("My Program")
root.mainloop()
Use self.label['text'] to change text
(Minimal?) Working example:
import tkinter as tk
class Window(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master)
# tk.Frame.__init__ creates self.master so you don't have to
#self.master = master
self.init_window()
def init_window(self):
self.pack(fill=tk.BOTH, expand=1)
quit_button = tk.Button(self, text="Exit", command=root.destroy)
start_button = tk.Button(self, text="Browse", command=self.on_click)
self.label = tk.Label(self, text="Hello")
quit_button.grid(row=0, column=0)
start_button.grid(row=0, column=1)
self.label.grid(row=1, column=0, columnspan=2)
def on_click(self):
self.label['text'] = "Starting..."
root = tk.Tk()
app = Window(root)
root.mainloop()
I'm learning about Tkinter and trying to create a normal looking chat style window. However, when resizing the window there are some unexpected results, such as a gray frame appearing between the items, despite setting expand=1.
Also how can I set it so the space is shared when resizing the window to be smaller than the original?
Here is my code:
from Tkinter import *
class Example(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initUI()
def initUI(self):
frame = Frame(self, relief=RAISED, borderwidth=1)
scrollbar = Scrollbar(self)
scrollbar.pack(side=RIGHT, fill=Y)
self.msgfield = Text(self, wrap=WORD, yscrollcommand=scrollbar.set)
self.msgfield.pack(side=TOP, fill=BOTH, expand=1)
self.msgfield.config(state=DISABLED)
self.inputbox = Text(self, height=2, width=30)
self.inputbox.pack(fill=BOTH, side=BOTTOM, expand=0)
self.inputbox.bind('<Return>', self.retrieve_input)
frame.pack(fill=BOTH, expand=1, side=BOTTOM)
self.pack(fill=BOTH, expand=1, side=TOP)
def retrieve_input(self, event):
msg = self.inputbox.get(1.0, END)[:-1]
self.msgfield.config(state=NORMAL)
self.msgfield.insert(END, msg)
self.msgfield.see(END) # Scroll if necessary
self.msgfield.config(state=DISABLED)
self.inputbox.delete(0.0, END)
def main():
root = Tk()
root.geometry("300x400+300+300")
app = Example(root)
root.mainloop()
if __name__ == '__main__':
main()
The issue is that you are packing both your msgfield and inputbox into self instead of frame, and self is the Tk instance which acts a bit different than a Frame. Try packing into your frame and I think you'll get the behaviour you want (note I added a black border to the Text widget so I could see it):
from Tkinter import *
class Example(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
frame = Frame(self, relief=RAISED, borderwidth=1)
scrollbar = Scrollbar(self)
scrollbar.pack(side=RIGHT, fill=Y)
self.msgfield = Text(frame, wrap=WORD, yscrollcommand=scrollbar.set)
self.msgfield.pack(side=TOP, fill=BOTH, expand=1)
self.msgfield.config(state=DISABLED)
self.inputbox = Text(frame, height=2, width=30, borderwidth=2)
self.inputbox.config(highlightbackground="Black")
self.inputbox.pack(fill=BOTH, side=BOTTOM, expand=0)
self.inputbox.bind('<Return>', self.retrieve_input)
frame.pack(fill=BOTH, expand=1, side=BOTTOM)
self.pack(fill=BOTH, expand=1)
def retrieve_input(self, event):
msg = self.inputbox.get(1.0, END)[:-1]
self.msgfield.config(state=NORMAL)
self.msgfield.insert(END, msg)
self.msgfield.see(END) # Scroll if necessary
self.msgfield.config(state=DISABLED)
self.inputbox.delete(0.0, END)
def main():
root = Tk()
root.geometry("300x400+300+300")
app = Example(root)
app.mainloop()
if __name__ == '__main__':
main()
I'm trying to create a root window with a black background to blend with my button backgrounds.
I have the following:
class Application(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initUI()
...
def initUI(self):
self.outputBox = Text(bg='black', fg='green', relief=SUNKEN, yscrollcommand='TRUE')
self.outputBox.pack(fill='both', expand=True)
self.button1 = Button(self, text='button1', width=40, bg ='black', fg='green', activebackground='black', activeforeground='green')
self.button1.pack(side=RIGHT, padx=5, pady=5)
self.button2 = Button(self, text='button2', width=20, bg='black', fg='green', activebackground='black', activeforeground='green')
self.button2.pack(side=LEFT, padx=5, pady=5)
...
def main():
root = Tk()
root.geometry('1100x350+500+300')
root.configure(background = 'black')
app = Application(root)
root.mainloop()
But root.configure(background = 'black') isn't changing the root window's background color... any suggestions?
This works (Check how parent root is referenced):
Edit: I edited the code and figure to make clear where colors are set:
from Tkinter import *
class Application(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.parent = master
self.initUI()
def initUI(self):
self.outputBox = Text(self.parent, bg='yellow', height= 10, fg='green', relief=SUNKEN, yscrollcommand='TRUE')
self.outputBox.pack(fill='both', expand=True)
self.button1 = Button(self.parent, text='button1', width=20, bg ='blue', fg='green', activebackground='black', activeforeground='green')
self.button1.pack(side=RIGHT, padx=5, pady=5)
self.button2 = Button(self.parent, text='button2', width=25, bg='white', fg='green', activebackground='black', activeforeground='green')
self.button2.pack(side=LEFT, padx=5, pady=5)
def main():
root = Tk()
app = Application(root)
app.parent.geometry('300x200+100+100')
app.parent.configure(background = 'red')
app.mainloop()
main()
Its 'bg' not 'background' in the .configure line.