How to set scrollText border in tkinter? - python

I am new to tkinter.
My code likes this,
import tkinter
from tkinter import scrolledtext
Win = tkinter.Tk()
Text = scrolledtext.ScrolledText(Win)
Text.pack(padx=10,pady=10)
Win.mainloop()
As you can see,it only have the left border,top border.
However,it haven't right border and bottom border.
I have watched this How to set border color of certain Tkinter widgets?.
And I have tried highlightbackground=color,
the code is this
import tkinter
from tkinter import scrolledtext
Win = tkinter.Tk()
Text = scrolledtext.ScrolledText(Win)
Text.config(highlightbackground="black")
Text.pack(padx=10,pady=10)
Win.mainloop()
it also didn't work.it made no difference.
In this document:tkinter.scrolledtext — Scrolled Text Widget,I know that the constructor is the same as that of the tkinter.Text class.And I have seen The Tkinter Text Widget,But it didn't have config about the tkinter.Text border.
What should I do?

it only have the left border,top border.
Because the widget config is relief="sunken",
So you can try relief="solid".
So this is may solve your problem
import tkinter
from tkinter import scrolledtext
Win = tkinter.Tk()
Text = scrolledtext.ScrolledText(Win,relief="solid")
Text.pack(padx=10,pady=10)
Win.mainloop()

Related

Tkinter (TTK) - How can I change the border color for the TreeView widgets?

I tried to change the default treeview border color using the highlightbackground option but it didn't work (my OS is Windows 10). here is an example:
from tkinter import *
from tkinter import ttk
parent=Tk()
style=ttk.Style()
style.configure("Treeview", highlightbackground="#red") # it doesn't work!!
TreeView = ttk.Treeview(parent)
TreeView.pack()
parent.mainloop()
How can I reach my goal?

Tkinter - How can I remove the background borders from ttk.Button?

by default, the buttons from ttk module, have a one pixel border. how can I delete this border? below an example code:
from tkinter import *
from tkinter import ttk
root = Tk()
root.geometry("400x300")
style=ttk.Style()
style.configure("TButton", padding=0, background="#ffffff") # I colored the background border white to see it in the window.. but, I don't want it, I want to delete the border!
MyButton = ttk.Button(root, text="I have a white border.. how can I delet it?")
MyButton.place(x=16, y=16)
root.mainloop()
I don't want to change the border color in order to hide it in the window,I want to delete it.

How to remove white lines around application tabs?

How to remove white lines around ttk.Notebook Tab window? I want a similar color as the main window.
import tkinter as tk
from tkinter import ttk
win=tk.Tk()
win.geometry("800x600")
win.title("Login and sinup")
canvas=tk.Canvas(win,bg="#303030",width=800,height=600)
canvas.pack()
tabs=ttk.Notebook(canvas)
tabs.pack()
tab1=tk.Frame(tabs,width=650,height=550,bg="#303030")
tabs.add(tab1,text=" New ")
tabs.place(x=100,y=100)
tabs.config()
win.mainloop()

Tkinter : How to center the window title

I am creating a project using tkinter and when I create a window, I couldn't seem to get the window title to center itself (Like most programs nowadays). Here's the example code:
from tkinter import *
root = Tk()
root.title("Window Title".center(110))# Doesn't seem to work
root.mainloop()
Is there a way to center the window title up ? Thanks in advance
There is nothing you can do. Tkinter has no control over how the window manager or OS displays the titles of windows other than to specify the text.
I came up with a trick that does the job and it consists in simply adding as much blank space before the title:
import tkinter as tk
root = tk.Tk()
root.title(" Window Title")# Add the blank space
frame = tk.Frame(root, width=800, height=200, bg='yellow')
frame.grid(row=0,column=0)
root.mainloop()
Output:
Alternatively, you can use a string consisting of an empty space and concatenate it to the title after multiplication. I mean:
import tkinter as tk
root = tk.Tk()
blank_space =" " # One empty space
root.title(80*blank_space+"Window Title")# Easier to add the blank space
frame = tk.Frame(root, width=800, height=200, bg='yellow')
frame.grid(row=0,column=0)
root.mainloop()
More adding onto what Billal suggested is this example that adjust depending on the window size. I still wouldn't recommend it since it's just a hack for visual aesthetics but if you really want to have it.
import tkinter as tk
def center(e):
w = int(root.winfo_width() / 3.5) # get root width and scale it ( in pixels )
s = 'Hello Word'.rjust(w//2)
root.title(s)
root = tk.Tk()
root.bind("<Configure>", center) # called when window resized
root.mainloop()
width=root.winfo_screenwidth()
spacer=(" "*(int(width)//6))
root.title(spacer+"Your title")
This is not that much perfect but this will work.

ttk:Entry widget disabled background color

I have a ttk Entry which is in "disabled" state.
The background color of the entry field while disabled is a light blue shade.
How can i change it to the default grey color? From this post i understood how we can change the foreground color.
tkinter ttk Entry widget -disabledforeground
I tried the same method for background color and it did not work.
I am using python 2.7 in Windows 7.
This is the code i tried as per the above said post:
from Tkinter import *
from ttk import *
root=Tk()
style=Style()
style.map("TEntry",background=[("active", "black"), ("disabled", "red")])
entry_var=StringVar()
entry=Entry(root,textvariable=entry_var,state='disabled')
entry.pack()
entry_var.set('test')
root.mainloop()
You don't need to use styles. You can change the color of disabled entry with option disabledbackground=<color>. You can use this option when creating the entry , like:
entry.config(background="black",disabledbackground="red")
So you overall code(Example) is:
from tkinter import *
import time
root=Tk()
entry=Entry(root,state='disabled')
entry.config(background="black",disabledbackground="red")
entry.pack()
root.mainloop()
Here is a screenshot of the GUI:
In ttk and Tk entries widgets, background refers to different things. In Tk Entry, background refers to the color behind the text, in ttk entry, background refers to the color behind the widget. (Yes, I know, confusing right?), what you want to change is fieldbackground. So your code would be
from Tkinter import *
from ttk import *
root=Tk()
style=Style()
style.map("TEntry",fieldbackground=[("active", "black"), ("disabled", "red")])
entry_var=StringVar()
entry=Entry(root,textvariable=entry_var,state='disabled')
entry.pack()
entry_var.set('test')
root.mainloop()

Categories