How to change Back ground colour for Tkinter module? - python-3.x

Hello I am very new with the python module Tkinter, I have written the code for a simple text editor. But I am not able to figure out how to change the background colour.
Help would be welcome thx.
Code:
from tkinter import *
import tkinter as tk
# from tkinter import filedialog
# from tkinter import font
root = Tk()
root.title('Flax')
root.iconbitmap('E:\editor.ico')
root.geometry('1200x660')
# Main Frame
my_frame = Frame(root)
my_frame.pack(pady=5)
# ScrollBar
scroll = Scrollbar(my_frame)
scroll.pack(side=RIGHT, fill=Y)
# Text Box
text = Text(my_frame, width=98, height=25, font=("Helvetica", 13), selectbackground="grey", selectforeground="white", undo=True)
text.pack()
scroll.config(command=text.yview)
root.mainloop()

The parameter you need to use is bg = "blue" for example source.
Implemented into your code:
from tkinter import *
import tkinter as tk
# from tkinter import filedialog
# from tkinter import font
root = Tk()
root.title('Flax')
root.iconbitmap('E:\editor.ico')
root.geometry('1200x660')
# Main Frame
my_frame = Frame(root)
my_frame.pack(pady=5)
# ScrollBar
scroll = Scrollbar(my_frame)
scroll.pack(side=RIGHT, fill=Y)
# Text Box
text = Text(my_frame, width=98, height=25, font=("Helvetica", 13), bg="grey", fg="white", undo=True)
text.pack()
scroll.config(command=text.yview)
root.mainloop()

Related

Inside a ttk Radiobutton, how to center the text? (anchor not working)

tkinker.ttk justifyandanchor is not working for ttk.Radiobutton.
This is my code:
from tkinter import *
from tkinter import ttk
window = Tk()
style = ttk.Style()
sty = ttk.Style(window)
op=IntVar(master=window)
sty.configure("TRadiobutton", background="green",foreground="red", anchor="center",justify='center')
entry_0 = ttk.Radiobutton(window,text="text",value=1,variable=op,style="white.TRadiobutton")
entry_0.place(
x=0,
y=0,
anchor="nw",
width=150
)
mainloop()
This is what I get:
This is what I except:
PS: I can use anchor=center in a tk widget.
As for you at entry level. You cannot jump to the immediate level. You have to lean step by step. Here is a newbie tutorial:
from tkinter import *
from tkinter import ttk
window = Tk()
style = ttk.Style()
sty = ttk.Style(window)
op=IntVar(master=window)
sty.configure("TRadiobutton", background="green",
foreground="red", anchor="center",
justify='center')
entry_0 = ttk.Radiobutton(window,text="text",value=1,variable=op,
style="white.TRadiobutton").pack(side = TOP)
mainloop()

Python: TypeError: 'Label' object is not callable

from PIL import Image, ImageTk
from tkinter import *
from tkinter import Label
def open_window():
menu = Toplevel(root)
menu.geometry("800x800")
menu.title("my game's menu")
menu.resizable(False, False)
menu.geometry("800x800")
lbl = Label(menu, text ="Hello!").pack
menu.mainloop()
root = Tk()
root.geometry("400x300")
Label = Label(root, text="Are you ready?")
Label.pack()
root.title("quick question")
btn = Button(root, text="Yes", command= open_window)
btn.pack(padx=20, pady = 20)
root.mainloop()
I got this error while I was working on previously seen code: File "C:\Users\User\Desktop\naujas zaidimas\scratch.py", line 11, in open_window
lbl = Label(menu, text ="Hello!").pack
TypeError: 'Label' object is not callable
Does anyone know why/how to fix it?
The code has a number of fairly obvious errors and one insidious error
This import plays no role in current code
from PIL import Image, ImageTk
Not a good or preferred way to import tkinter
This will cause problems later on
from tkinter import *
This is unnecessary with the current inport method
from tkinter import Label
def open_window():
The rule for Python functions is: Names created in functions stay in functions.
It will require a global instruction to make 'menu' available elsewhere in your code
menu = Toplevel(root)
menu.geometry("800x800")
menu.title("my game's menu")
menu.resizable(False, False)
Duplicated geometry instruction
menu.geometry("800x800")
This object has already been defined as a Label object so trying to name it throws a TypeError
lbl = Label(menu, text ="Hello!").pack()
This is unnecessary since root.mainloop() has already been executed
menu.mainloop()
root = Tk()
root.geometry("400x300")
Here is another naming problem caused by the import method chosen
Label = Label(root, text="Are you ready?")
Label.pack()
root.title("quick question")
This button will enable you to create MANY Toplevel windows
The problem is, ALL of them will be called 'menu'!?
This is the insidious error
btn = Button(root, text="Yes", command= open_window)
btn.pack(padx=20, pady = 20)
root.mainloop()
# This solution avoids all the previous problems
import tkinter as tk
root = tk.Tk()
root.title("quick question")
root.geometry("400x300")
tk.Label(root, text = "Are you ready?").pack()
# This will give tkinter time to process the given instructions.
root.update()
menu = tk.Toplevel(root)
# withdraw will make menu temporarily invisible
menu.withdraw()
menu.title("my game's menu")
menu.geometry("800x800")
menu.resizable(False, False)
tk.Label(menu, text = "Hello!").pack()
# command will now make menu window visible
btn = tk.Button(root, text = "Yes", command = menu.deiconify)
btn.pack(padx = 20, pady = 20)
root.mainloop()
from PIL import Image, ImageTk
from tkinter import *
def open_window():
menu = Toplevel(root)
menu.geometry("800x800")
menu.title("my game's menu")
menu.resizable(False, False)
menu.geometry("800x800")
lbl = Label(menu, text ="Hello!").pack()
menu.mainloop()
root = Tk()
root.geometry("400x300")
lbl1 = Label(root, text="Are you ready?").pack()
root.title("quick question")
btn = Button(root, text="Yes", command= open_window)
btn.pack(padx=20, pady = 20)
root.mainloop()
I Played around both our codes and now somewhy it works.

Changing the background of ttk themed widgets in python

I am currently trying to learn ttk themed widgets. I wanted to change the background colour of my ttk button. I followed to ttk docs and written this:
from tkinter import *
from tkinter.ttk import *
root = Tk()
root.title("GUI App One")
root.geometry("800x500")
root.maxsize(800,500)
root.minsize(800,500)
Style().configure("C.TButton", padding=6, background="blue", relief="raised")
Style().configure("B.TButton", font=("Arial",30))
Style().configure("Elem.TFrame", background="red")
backframe = Frame(root, width=800, height=500, style="Elem.TFrame")
backframe.place(x=0, y=0)
print()
btn1 = Button(backframe, text="Click me", style="C.TButton")
btn1.place(x=20, y=50)
btn2 = Button(backframe, text="Click me too", style="B.TButton")
btn2.place(x=100, y=100)
mainloop()
in 'C.TButton' styling I tried to change the background colour of the 'btn1', but it only changes border colour to blue and not the background colour. How can I change the background colour?

Says my button for calculator should resolve but doesn't

Button section says tk should resolve but doesn't. Also it doesn't recognize the frame variable in the brackets (trying to pack it in frame)
Code:
import tkinter as tk
from tkinter import *
from tkinter import font
root = tk.Tk()
canvas = tk.Canvas(root, height=700, width=700, bg='SkyBlue1')
canvas.pack()
fnt = font.Font(family='Arial', size=60, weight=font.BOLD)
title = tk.Label(canvas, text="ProjX", bg='SkyBlue1', font=fnt)
title.pack()
root.configure(bg='CadetBlue1')
frame = tk.Frame(root, bg='DeepSkyBlue2')
frame.place(relwidth='0.8', relheight='0.6', relx='0.1', rely='0.2'
calc = tk.Button( frame, text="Suvat Calculator", bg='SkyBlue1', font='Arial' )
calc.pack()
root.mainloop()

Tkinter Label Text Not Appearing

I use this code (from https://www.python-course.eu/tkinter_labels.php) but no text appears in the label.
import tkinter as tk
root = tk.Tk()
w = tk.Label(root, text="Hello Tkinter!")
w.pack()
root.mainloop()
I suspect that any problem may involve the mainloop function.

Resources