import tkinter as tk
from tkinter import filedialog
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg, NavigationToolbar2Tk)
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
from data import dataprocess
root = tk.Tk()
root.geometry('1500x800')
container = tk.Frame(root)
main_canvas = tk.Canvas(container, width=1480, height=800, bg='blue')
scrollbar = tk.Scrollbar(container, orient="vertical", command=main_canvas.yview)
scrollable_frame = tk.Frame(main_canvas, bg='black')
scrollable_frame.bind(
"<Configure>",
lambda e: main_canvas.configure(
scrollregion=main_canvas.bbox("all")
)
)
main_canvas.create_window((0, 0), window=scrollable_frame, anchor="nw")
main_canvas.configure(yscrollcommand=scrollbar.set)
for i in range(1400):
btn = tk.Button(scrollable_frame, text='btn' + str(i)).grid(row=i, column=0)
container.grid()
main_canvas.grid()
scrollbar.grid(row=0, column=7, sticky="nse")
[enter image description here][1]container.mainloop()
My buttons can not show inside the frame, seems like my frame can not afford that much widgets.
Is there any method to fix it?
[1]: https://i.stack.imgur.com/qgZGh.png
Related
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()
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()
This question already has answers here:
Why does Tkinter image not show up if created in a function?
(5 answers)
Closed 2 years ago.
I simply want to show an image in a label widget. I managed the task by the following code:
import tkinter as tk
from PIL import Image,ImageTk
import requests
root=tk.Tk()
url="https://upload.wikimedia.org/wikipedia/commons/thumb/e/e0/SNice.svg/500px-SNice.svg.png"
imageurl=requests.get(url, stream=True)
image=Image.open(imageurl.raw)
img = ImageTk.PhotoImage(image.resize(size=(500,500)))
origPictureBox = tk.Label(root, image=img, anchor=tk.NW, height=500, width=500)
origPictureBox.config(image=img)
origPictureBox.pack(side=tk.TOP)
root.mainloop()
However, when I put the main tasks into a function, the picture is not displayed:
import tkinter as tk
from PIL import Image,ImageTk
import requests
def initUI(root):
url="https://upload.wikimedia.org/wikipedia/commons/thumb/e/e0/SNice.svg/500px-SNice.svg.png"
imageurl=requests.get(url, stream=True)
image=Image.open(imageurl.raw)
img = ImageTk.PhotoImage(image.resize(size=(500,500)))
origPictureBox = tk.Label(root, image=img, anchor=tk.NW, height=500, width=500)
origPictureBox.config(image=img)
origPictureBox.pack(side=tk.TOP)
root=tk.Tk()
initUI(root)
root.mainloop()
Can somebody explain this behavior? If other items are to be displayed (buttons, etc.), they are visible, it's just the image that hides.
The same happens if I use a class instead of the function initUI.
It's my first question in stackOverflow and I am sorry, if the answer is obvious.
You need to keep reference to the image, or its get garbage collected
So add:
l = tk.Label(root)
l.image = img
Full code
import tkinter as tk
from PIL import Image,ImageTk
import requests
def initUI(root):
url="https://upload.wikimedia.org/wikipedia/commons/thumb/e/e0/SNice.svg/500px-SNice.svg.png"
imageurl=requests.get(url, stream=True)
image=Image.open(imageurl.raw)
img = ImageTk.PhotoImage(image.resize(size=(500,500)))
l = tk.Label(root)
l.image = img
origPictureBox = tk.Label(root, image=img, anchor=tk.NW, height=500, width=500)
origPictureBox.config(image=img)
origPictureBox.pack(side=tk.TOP)
root=tk.Tk()
initUI(root)
root.mainloop()
I want to show the plot canvas in the top right side of UI (using Tkinter python3). I wrote the following code but I got the following error:
canvas = FigureCanvasTkAgg(fig, master=root) NameError: name 'root' is not defined
My code is:
import tkinter as tk
import tkinter.ttk as ttk
from tkinter import *
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.figure import Figure
import sys
class GUI(tk.Frame):
def __init__(self, master = None):
self.root = tk.Tk()
self.root.geometry("500x500")
tk.Frame.__init__(self, master)
self.createWidgets()
def start(self):
self.root.mainloop()
def createWidgets(self):
fig = plt.figure(figsize=(8, 8))
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.get_tk_widget().grid(row=0, column=1)
canvas.show()
def main():
appstart = GUI()
appstart.start()
if __name__ == "__main__":
main()
I made some changes to your code, deleting some unnecessary
references such as:
import tkinter.ttk as ttk
from tkinter import *
from matplotlib.figure import Figure
import sys
self.root = tk.Tk()
tk.Frame.__init__(self, master)
that you dont' use and I think only confuse yourself.
I've even simplified your script and add this code below to show to you wath is "self"
print(type(self))
for item in dir(self):
print(type(item),item)
I've add even a toolbar to the plot and some data to plot something.
Regards
import tkinter as tk
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
import numpy as np
class GUI(tk.Tk):
def __init__(self):
super().__init__()
self.title("Hello World")
self.geometry("500x500")
self.createWidgets()
print(type(self))
for item in dir(self):
print(type(item),item)
def createWidgets(self):
t = np.arange(0, 3, .01)
f0 = tk.Frame()
fig = plt.figure(figsize=(8, 8))
fig.add_subplot(111).plot(t, 2 * np.sin(2 * np.pi * t))
canvas = FigureCanvasTkAgg(fig, f0)
toolbar = NavigationToolbar2Tk(canvas, f0)
toolbar.update()
canvas._tkcanvas.pack(fill=tk.BOTH, expand=1)
f0.pack(fill=tk.BOTH, expand=1)
def main():
appstart = GUI()
appstart.mainloop()
if __name__ == "__main__":
main()
In your code you should use self.root if your intent is to use the root window:
canvas = FigureCanvasTkAgg(fig, master=self.root)
... or maybe just self, if your intent is to have it appear inside the frame. I'm not entirely sure what you are intending to do
canvas = FigureCanvasTkAgg(fig, master=self)
Somewhat unrelated, if you're explicitly creating a root window, you should be passing that to tk.Frame.__init__ rather than master.
tk.Frame.__init__(self, self.root)
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()