Powered by Blogger.
  • Home
  • About
  • Web Development
  • Android
  • Python
  • Java
facebook twitter instagram pinterest bloglovin Email

CodeUniversal

This might sound a little bit flashy because no one ever thought of creating a software with the button right in front of the your text area or somewhere in the middle of an image label, where the nose of the person in the image is clickeable. You can make some silly game out of this, or a cool one if you want to.
Again, you can tank me later for that. :)

You should create canvas so that the button can depend on it as a parent or root and then use the place manager to place it anywhere you want within the realm of possibilities.

Here's the code. You figure the rest out...

from Tkinter import *
window = Tk()
frame = Frame(window).pack()
canvas = Canvas(frame).pack(side = LEFT)
text = Text(frame,wrap = WORD)
text.insert(END,"THIS IS THE TEXT AREA AND WE ARE PLACING THE BUTTON IN THE MIDDLE");
text.pack()
framec = Frame(canvas).pack()
button = Button(framec, text = "Button").place(x = 500,y=12)
window.mainloop()

Here's a screenshot of what the code basically does.



Please do not hesitate to contact and ask me anything you want. Especially, in Python and Java. I will try to assist.

Comment below if you have any questions or comments.

Thank you!
Share
Tweet
Pin
Share
No comments
I think this trick shouldn't be shared to the masses for free. But i'm just gonna show it to you and you can thank me later.

Here's what I managed to do.
While I was writing a python code for one of my softwares, Banattend, I was forced to use a list of images in a canvas. But then, I didn't know how to scroll down. Until, I found out that I can embed my canvas in a frame and then add a frame in the canvas, technically the canvas can't be related to scrollbar, and that's why I use the second frame. Sounds simple, huh!?!

Most people may try to tell you to use the update() or idle_task functions, which from my experiences, don't really do anything important for your code. So follow this trick below.

First, create a root window and then add a frame inside the window. Then embed a canvas in that frame, and finally put another frame for that canvas. Then attach the scrollbar to the first frame.

In our case, we create myframe in root window and then canvas to be a part of myframe and we insert a frame by the name window, to enable us scroll down and add our objects there. You can basically add images in the space and scroll them down to see your list of pictures or objects.

Here's a sample code.

from Tkinter import *

root = Tk()
sizex = 450
sizey = 300
posx  = 100
posy  = 100
root.wm_geometry("%dx%d+%d+%d" % (sizex, sizey, posx, posy))
myframe=Frame(root,relief=GROOVE,width=50,height=50,bd=1,bg = "yellow")
myframe.place(x=10,y=30)

def myfunction(event):
    canvas.configure(scrollregion=canvas.bbox("all"),width=200,height=150)
news = "hi you can replace this with an image"
canvas=Canvas(myframe,bg = "yellow")
window=Frame(canvas,bg = "yellow")
myscrollbar=Scrollbar(myframe,orient="vertical",command=canvas.yview)
canvas.configure(yscrollcommand=myscrollbar.set)
label = Label(window, text = news).pack()
label = Label(window, text = news).pack()
label = Label(window, text = news).pack()
label = Label(window, text = news).pack()
label = Label(window, text = news).pack()
label = Label(window, text = news).pack()
label = Label(window, text = news).pack()
label = Label(window, text = news).pack()
label = Label(window, text = news).pack()
label = Label(window, text = news).pack()
myscrollbar.pack(side="right",fill="y")
canvas.pack(side="left")
canvas.create_window((0,0),window=window,anchor='nw')
window.bind("<Configure>",myfunction)
root.mainloop()

Enjoy your coding experience.

Do not hesitate to comment below if you have any questions or improvements.

Thanks!
Share
Tweet
Pin
Share
No comments
Many people have been asking how they can add a button so that their tkinter GUI can refresh itself. The first time I wanted to add a refresh button, I thought this was almost impossible because everyone thinks it's not something python has to offer. But I can show you how I managed to add a refresh button into my python code. I am being tempted to think I'm the only one to discover this trick, so if you're going to use it anywhere, don't forget to thank me. :) lol. This is super easy!

Here's how I managed to do it,

1. First create your first tkinter code with a refresh button in it.

Just for the sake of example, let's use the datetime function to see if the time changes everytime we click on the refresh button. If the second changes per click, it means we have successfully created our refresh button.

Save the file as file.pyw

from Tkinter import *
import datetime
now = datetime.datetime.now()
seconds = now.second
window = Tk()
def refresh():
       window.destroy()
       execfile("file.pyw",globals())       
label = Label(window, text = str(seconds))
button = Button(window, text = "Click here to refresh",command = refresh)
label.pack()
button.pack()
window.mainloop()


That's it! Now your tkinter has to refresh itself when you click on it.

Do not hesitate to Comment and ask your questions below and I'll try to answer them all.

Good luck with your coding experience!
Share
Tweet
Pin
Share
6 comments

Follow Us

  • facebook
  • youtube

Featured Post

Servlets and JSP tutorial - Installing apache and glassfish server on netbeans

To use servlets and JSP, we either need eclipse or netbeans. I prefer netbeans and in this tutorial we'll see how to set up servlets an...

Sponsor

Facebook

Popular Posts

  • How to add a refresh button in Python Tkinter GUI
    Many people have been asking how they can add a button so that their tkinter GUI can refresh itself. The first time I wanted to add a refre...
  • Adding a scrollbar to canvas images for Python Tkinter
    I think this trick shouldn't be shared to the masses for free. But i'm just gonna show it to you and you can thank me later. Here...
  • Placing a button in the middle of text areas and anywhere. (Tkinter, Python)
    This might sound a little bit flashy because no one ever thought of creating a software with the button right in front of the your text are...
  • Servlets and JSP tutorial - Installing apache and glassfish server on netbeans
    To use servlets and JSP, we either need eclipse or netbeans. I prefer netbeans and in this tutorial we'll see how to set up servlets an...

About me

About Me

Aenean sollicitudin, lorem quis bibendum auctor, nisi elit conseat ipsum, nec sagittis sem nibh id elit. Duis sed odio sit amei.

Created with by ThemeXpose