how to lift/raise a tkinter button python - python

I'm trying to lift a Tkinter button so it overlaps a scrolltext in tkinter, I've tried
button.lift()
button.tag_raise
and I've also tried to make the other widget I want to be under the button lower by using
widget.lower()
widget.tag_lower()
but none of these seem to work for me, it may be because scrolltext is not a fully official tkinter widget and that may cause issues, but if anyone can help please do so! Thank you.
from tkinter import *
from tkinter import font
from tkinter.scrolledtext import ScrolledText
save_button = Button(main, fg="white",
bg="#121212", text="SAVE", font=("Microsoft JHengHei", 10),
border=0, activeforeground="white", activebackground="#141414",
relief=FLAT,
width=12,
command=save_textfile)
save.pack()
scrolltext = scrolledtext.ScrolledText(window, height=11, width=60, bg="#050505", fg="white", bd=0)
scrolltext.pack()
#this should lower the scrolltext
scrolltext.lower()
#and this should lift the save button
save_button.lift()
neither of these work.

Related

Tkinter Toplevel inf different file/function

I am trying to create a Toplevel window, however, this Toplevel is called from a different file in the same directory within a function.
Apologies I am by no means a tkinter or python guru. Here are the two parts of the code. (snippets)
#File 1 (Main)
import tkinter as tk
from tkinter import *
import comm1
from comm1 import com1
root = tk.Tk()
root.title("")
root.geometry("1900x1314")
#grid Center && 3x6 configuration for correct gui layout
root.grid_rowconfigure(0, weight=1)
root.grid_rowconfigure(11, weight=1)
root.grid_columnconfigure(0, weight=1)
root.grid_columnconfigure(11, weight=1)
#background image
canvas = Canvas(root, width=1900, height=1314)
canvas.place(x=0, y=0, relwidth=1, relheight=1)
bckground = PhotoImage(file='img.png')
canvas.create_image(20 ,20 ,anchor=NW, image=bckground)
#command to create new Toplevel
btn1 = tk.Button(root, text='Top', command=com1, justify='center', font=("Arial", 10))
btn1.config(anchor=CENTER)
btn1.grid(row=4, column=1)
#File 2 (Toplevel)
#command for new window
def com1():
newWindow1 = Toplevel(root)
newWindow1.title("")
newWindow1.geometry("500x500")
entry1 = tk.Entry(root, justify='center' , font=("Arial", 12), fg="Grey")
newWindow1.pack()
newWindow1.mainloop()
The weird part is this worked perfectly for a few minutes and without changing any code it just stopped working.
Where am I going wrong?
You need to pass root as an argument to com1
Also, you only need to start mainloop once, and that should probably be in the main file. You do not need to call it each time you create a new window.
Thanks everyone that answered,
Decided to bypass the problem with better structuring in a single file. :)

Trying to write a tkinter popup with dynamic text and confirmation button

I am trying to make a popup for messages such as "Action completed" and I want a button on it to close the popup (A simple OK which quits only the popup). It also sometimes moves behind the window which is annoying. I have some code for the button but it has an issue with the geometry of the shape since the shape isn't exactly defined as it is variable through the size of font and text length.
import tkinter as tk
from tkinter import *
import pygame
import sys
def Text(Text_input, Font, Background, Foreground):
my_window = tk.Tk()
my_window.title(Text_input)
my_window.geometry()
help_label = tk.Label(my_window, font = Font, bg=Background, fg=Foreground, text = Text_input)
help_label.grid(row=0, column=0)
button = tk.Button(my_window, text="QUIT", fg="red", command=quit)
button.pack(side=tk.BOTTOM)
my_window.mainloop()
Text("Hello", "calibri 80", "white", "black")
From my own experience wanting to achieve this, I wrote a simple tkinter code export_success.py as follows:
from tkinter import *
from tkinter import ttk
import sqlite3
from tkinter.ttk import *
root = Tk()
root.geometry('280x100')
root.title("Export Status")
root.config(bg="")
style = ttk.Style()
label_0 = Label(root, text="Export Successful", width=20, background="white", foreground="grey15", font=("Arial, bold", 15)).place(x=50, y=23)
exit1 = Button(root, text='Exit', style='C.TButton', width=11, command=root.destroy).place(x=100, y=60)
root.mainloop()
I then just insert this code os.system('python export_success.py') at the end of my tkinker window that I'm working on.
In this case I'm exporting my information and wanted to know when it is successful.
I am not saying this is best practice and there might be other ways, I just found it to work out for me.

Tkinter overrideredirect not working in Mac OS X

I'm using the latest version of Mac OS, and Python 3.9. I tried using the overrideredirect to delete the title bar and add my own. However, the result does not show the window. The app is seen in the dock, and the menu bar. But it is not displayed.
My code:
from tkinter import *
root = Tk()
def move_window(event):
root.geometry('+{0}+{1}'.format(event.x_root, event.y_root))
root.overrideredirect(True)
root.geometry('400x100+200+200')
title_bar = Frame(root, bg='white', relief='raised', bd=2)
close_button = Button(title_bar, text='X', command=root.destroy)
window = Canvas(root, bg='black')
title_bar.pack(expand=1, fill=X)
close_button.pack(side=RIGHT)
window.pack(expand=1, fill=BOTH)
title_bar.bind('<B1-Motion>', move_window)
root.mainloop()
The same code works well in windows.
Thanks in advance! :)
On Macs, we need two calls of overrideredirect to remove the border.
Do it like this:
root.overrideredirect(False)
root.overrideredirect(True)
But note that certain events might fail to bind properly, per this post

How do you make scrolling pages in a tkinter gui with python 3, like on a website?

I am making a small program with the tkinter plugin in python. I want to make a page scrolling as you can do in the browser. Is it possible? and how do you do it?
window2 = Tk()
window2.title("RPG SM Beta 0.1")
window2.geometry('150x200')
window2.configure(background="#5C5858")
window2.iconbitmap("Logo.ico")
window2.resizable(False, False)
write1 = Text(window2, height=5, width=17, bg="white")
write1.place(x=6, y=30)
lab2 = Label(window2, text='Lorem ipsum', bg="#5C5858", fg='white', font='none 12 bold')
lab2.place(x=4, y=5)
The most command solution is to use a Canvas widget as the container. Anything added to a canvas with the create_window method will be scrollable.

ttk.Entry.select_range() works with Button, but not ttk.Button in python / tkinter?

Can some one please explain why entry.select_range() works with a Button, but not a ttk.Button?
from tkinter import *
from tkinter import ttk
root = Tk()
entry = ttk.Entry(root)
entry.pack()
#This works
button = Button(root, text="Select your text", command=lambda:
entry.select_range(0, END))
#but this doesn't
##button = ttk.Button(root, text="Select your text", command=lambda:
## entry.select_range(0, END))
button.pack()
root.mainloop()
This answer from Google Group says,
However, on Windows (only) the selection will only become visible
when the entry gets the focus.
and also this page about ttk button says,
By default, a ttk.Button will be included in focus traversal ... To
remove the widget from focus traversal, use takefocus=False
So you need to add takefocus option to ttk.Button.
button = ttk.Button(root, takefocus=False, text=...)

Categories