i am opening a file in a gui label.
the file i have is big, i need a scroll for it in gui frame.
the code i am using for loading the file and displaying it in a frame is
from Tkinter import *
tk = Tk()
tk.title("Vulnerability Report")
f = open("hola.txt", "r").read()
Label(tk, text=f) .grid(row=0)
tk.mainloop()
Here is the screenshot: I want a scroll here through which i can read the whole file up and down
I have no idea why you are using Canvas instead of Frame and why you are loading a large file on a Label, but you cannot attach a Scrollbar to a label. Tkinter has a ScrolledText widget. Do from tkinter.scrolledtext import ScrolledText and use it as one would a Text widget.
Related
Here is my code so far:
import os
from tkinter import *
import tkinter as tk
system_properties=("C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Administrative Tools\System Information.lnk")
device_manager=("devmgmt.msc")
root = Tk()
#root.title()
window = tk.Tk()
root.geometry("200x200")
def open_app():
os.startfile(system_properties)
os.system(device_manager)
Button(root, text ='Open',
command = open_app).pack(side = TOP,
pady = 10)
root.mainloop()
To clarify I am not looking to change the tkinter window. What I want to do is open multiple applications at once on Windows 11 with no overlap like in a split screen format, but I am not sure how to incorporate code to make the windows display with my desired size and position. Currently, they are opening on top of each other with different window sizes.
So I have been using Tkinter and I have it set up with a base background. I then open a file that adds my buttons and everything else. I will then have more files for the login and the game so on so on.
My main issue is closing the file. I could just use place.forget but what I really want to do is close the active file. I tried using exit() but all that does is close the window. so in short i want to close the file with the buttons but not the background file.
file_name = background.py
from tkinter import *
from tkinter import messagebox
top=Tk()
top.geometry("700x500")
top.resizable(False,False)
c=Canvas(top,bg="gray16",height=200,width=200)
c.pack()
filename=PhotoImage(file="mountain.png")
background_label=Label(top,image=filename)
background_label.place(x=0,y=0,relwidth=1,relheight=1)
canvas=Canvas(width=400,height=330)
canvas.place(x=150,y=70)
exec(open("./Home_screen.py").read())
top.mainloop()
this basically creates a graphical background and main window
file_name = Home_screen.py
from tkinter import *
from tkinter import messagebox
def rem():
loginPage.place_forget()
loginPage=Button(text="Login", width=20, command=lambda:[rem()], border=2)
loginPage.place(x=271,y=270)
what I would like to do is remove the .place_forget and replace it by closing Home_screen.py
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()
I want to put an image in the second window using tkinter, in the first window the code works good, but the second window shows nothing.
In this part I import necessary modules:
from tkinter import filedialog, Tk, Frame, Label, PhotoImage, Button
from PIL import Image
from tkinter import*
import tkinter as tk
Then create the principal window:
raiz = Tk()
raiz.title("ventana")
Then I create the frame and put the image in the frame:
miFrame = Frame()
miFrame.pack()
miFrame.config(width="1400", heigh=("1200"))
fondo=tk.PhotoImage(file="fondoF.png")
fondo=fondo.subsample(1,1)
label=tk.Label(miFrame,image=fondo)
label.place(x=0,y=0,relwidth=1.0,relheight=1.0)
Then a button that will call the second window function:
btn3 = Button(raiz, text="boton")
btn3.place(x=500, y=500)
btn3.config(command=abrirventana2)
Here we have the function which opens the second window and here (I guess) is where I want to put the image.
This part also has two buttons named mih which does nothing in the meantime and ok which calls the function to close the second window:
def abrirventana2():
raiz.deiconify()
ventana2=tk.Toplevel()
ventana2.geometry('500x500')
ventana2.title("ventana2")
ventana2.configure(background="white")
fondov=tk.PhotoImage(file="xxx.gif")
label1=tk.Label(ventana2,image=fondov)
label1.place(x=50,y=50,relwidth=5.0,relheight=5.0)
mensaje=tk.Label(ventana2,text="funciona")
mensaje.pack(padx=5,pady=5,ipadx=5,ipady=5,fill=tk.X)
boton1=tk.Button(ventana2,text='mih')
boton1.pack(side=tk.TOP)
boton2=tk.Button(ventana2,text='ok',command=ventana2.destroy)
boton2.pack(side=tk.TOP)
Function to close the second window:
def cerrarventana2():
ventana.destroy()
I use the mainloop to keep the window open
raiz.mainloop()
Note: I had already tried creating a frame in the second window, but it didn't work.
Apologies for my previously incorrect answer.
The reason the image is not showing is due to the fact that you did not create a reference to it. If you don't create a reference, the image is garbage collected, which doesn't remove it, but in a sense just renders a blank placeholder on the GUI.
In order to display the image correctly you need to add a reference to the image within the code that displays the image.
You therefore now have:
fondov=tk.PhotoImage(file="giphy.gif")
label1=tk.Label(ventana2,image=fondov)
label1.image = fondov
label1.pack()
(label1.image = fondov is the reference)
Sorry for the confusion there. This should work.
Basically, I want the body of a Text widget to change when a StringVar does.
Short version is, you can't. At least, not without doing extra work. The text widget doesn't directly support a variable option.
If you want to do all the work yourself it's possible to set up a trace on a variable so that it keeps the text widget up to date, and you can add bindings to the text widget to keep the variable up to date, but there's nothing built directly into Tkinter to do that automatically.
The main reason this isn't directly supported is that the text widget can have more than just ascii text -- it can have different fonts and colors, embedded widgets and images, and tags.
I thought of this while working on a project. It's actually really simple:
import tkinter as tk
from tkinter import *
from tkinter.scrolledtext import ScrolledText
def get_stringvar(event):
SV.set(ST1.get("1.0", END))
ST2.replace("1.0", END, SV.get())
root = tk.Tk()
SV = StringVar()
ST1 = ScrolledText(root)
ST1.pack()
ST1.bind('<KeyRelease>', get_stringvar)
ST2 = ScrolledText(root)
ST2.pack()
root.mainloop()