Tkinter scrollbar is not having the dragable part - python

Code:
from tkinter import *
from tkinter import ttk
root=Tk()
# creating the main frame
main_frame=Frame(root)
main_frame.pack(fill=BOTH,expand=1)
mycanvas=Canvas(main_frame)
mycanvas.pack(side=LEFT,fill=BOTH,expand=1)
myscrollbar=Scrollbar(main_frame,orient=VERTICAL,command=mycanvas.yview)
myscrollbar.pack(side=RIGHT,fill=Y)
mycanvas.configure(yscrollcommand=myscrollbar.set)
mycanvas.bind("<Configure>",lambda e: mycanvas.configure(scrollregion=mycanvas.bbox("all")))
secondframe=Frame(mycanvas)
mycanvas.create_window((0,0),window=secondframe,anchor="nw")
labell=Label(secondframe,text="run this")
labell.grid(row=3,column=4)
root.mainloop()
Output:
Everything is working fine, except that dragable part to scroll. Please help me with that

This code will work just fine, all you have to do is have more items in the frame such that it needs to have be full to be dragged. To see it for yourself, change your label with:
for a in range(50):
Label(secondframe,text=a).grid(row=a,column=4)
You will notice the scrollbar can be used now. Without having alot of "items", there is no need for a y-scrollbar and hence it is grayed out.

Related

Removing Base Window When Using Tkinter Python 3 (Askopenfiledialogue)

This is my first post on here, so bear with me as far as post etiquette goes. I've been struggling to get rid of the base Tkinter window that appears when I'm using the askopenfilename function of Tkinter. I've tried using Withdraw and destroy (in combination and alone) to fix this issue, but it seems to leave my code stuck in a loop and unable to continue to the next sections.
I have seen several solutions to this in Python 2, but I have no idea how they translate to python 3. Tkinter isn't a module I have a lot of experience with, so it is likely a simple oversight I am making.
Any suggestions or comments are much appreciated
Here is a sample of my code I am using (CSV module is for another section of my code)
import csv
from tkinter import filedialog
from tkinter import *
root= Tk()
root.filename= filedialog.askopenfilename(initialdir = r"\Users",title="Select A File", filetypes= [("Csv Files","*.csv")])
root.mainloop()
Use withdraw to hide root window, deiconify to show root window
import csv
from tkinter import filedialog
from tkinter import *
root=Tk()
# Hide Window
root.withdraw()
filename= filedialog.askopenfilename(initialdir = r"\Users",title="Select A File", filetypes= [("Csv Files","*.csv")])
# Show Window
root.deiconify()
root.mainloop()

Python 3 tkinter: focus_force on messagebox

I'm running python 3 code in background which should show a popup window in some situations. I'm using tkinter for this:
import tkinter as tk
from tkinter import messagebox
def popup(message, title=None):
root = tk.Tk()
root.withdraw()
root.wm_attributes("-topmost", 1)
messagebox.showinfo(title, message, parent=root)
root.destroy()
popup('foo')
The ok-button in this infobox should get the focus automatically when popping up. Sadly I'm not able to do this. I tried root.focus(), but it does not help. Any ideas how to solve that? TIA
BTW: The code should be platform independent (Linux and Windows).
Edit:
Maybe I missunderstood the focus keyword and I should clarify my question:
root = tk.Tk()
root.focus_force()
root.wait_window()
When calling the code above the root window is active, even if I worked in e.g. the browser before. Is this also possible for messagebox.showinfo? Adding root.focus_force() in the popup function does not help.
Is this even possible? Or is it necessary to create my own root window? I really like the appearance of the messagebox with the icon.
Edit 2:
Here is a video: https://filebin.net/no195o9rjy3qq5c4/focus.mp4
The editor is the active window, even after the popup was shown.
In Linux I it works as expected.
You can use the default argument in the messagebox function.
default constant
Which button to make default: ABORT, RETRY, IGNORE, OK, CANCEL, YES, or NO (the constants are defined in the tkMessageBox module).
So, here is an example to highlight the "ok" button.
import tkinter as tk
from tkinter import messagebox
def popup(message, title=None):
root = tk.Tk()
root.withdraw()
messagebox.showinfo(title, message, parent=root, default = "ok")
root.destroy()
popup('foo')
Hope this helps!

Tkinter text widget - Why does INSERT not work as text index?

I have a problem that annoys me. I am currently building a small app with a Tkinter GUI.
On the front page, I want some introductory text in either a text or a scrolledtext widget. The code examples I've come across uses keywords such as INSERT, CURRENT and END for indexation inside the widget.
I have literally copy pasted the below code into my editor, but it doesn't recognise INSERT (throws error: "NameError: name 'INSERT' is not defined"):
import tkinter as tk
from tkinter import scrolledtext
window = tk.Tk()
window.title("test of scrolledtext and INSERT method")
window.geometry('350x200')
txt = scrolledtext.ScrolledText(window,width=40,height=10)
txt.insert(INSERT,'You text goes here')
txt.grid(column=0,row=0)
window.mainloop()
I can get the code to work if I change [INSERT] with [1.0], but it is very frustrating that I cannot get INSERT to work, as I've seen it in every example code I've come across
Use tk.INSERT instead of only INSERT. Full code is shown.
import tkinter as tk
from tkinter import scrolledtext
window = tk.Tk()
window.title("test of scrolledtext and INSERT method")
window.geometry('350x200')
txt = scrolledtext.ScrolledText(window,width=40,height=10)
txt.insert(tk.INSERT,'You text goes here')
txt.grid(column=0,row=0)
window.mainloop()
You don't need to use the tkinter constants. I personally think it's better to use the raw strings "insert", "end", etc. They are more flexible.
However, the reason the constants don't work for you is that you're not directly importing them. The way you're importing tkinter, you need to use tk.INSERT, etc.
INSERT could not be used directly.
You can use it in the past just because you used this in the past:
from tkinter import * # this is not a good practice
INSERT,CURRENT and END are in tkinter.constants.Now in your code,you even didn't import them.
If you want to use them,you can use
from tkinter.constants import * # not recommended
...
txt.insert(INSERT,'You text goes here')
Or
from tkinter import constants
...
txt.insert(constants.INSERT,'You text goes here') # recommend
If didn't want to import them,you can also use:
txt.insert("insert",'You text goes here')
Edit:I found in the source code of tkinter,it had import them,reboot's answer is also OK.

tkinter topmost and overridedirect

I want to write a program with a window set to always at the top and without the border. So I write the following program, which doesn't work as intended (macOS Sierra, 10.12.3):
import tkinter as tk
root=tk.Tk()
tk.Label(root,text='some text').pack()
root.attributes('-topmost',True)
root.overrideredirect(1)
root.mainloop() #this one doesn't work
screenshot of the failed one
However, when I change the sequence of overridedirect and attributes, surprisingly it worked.
import tkinter as tk
root=tk.Tk()
tk.Label(root,text='some text').pack()
root.overrideredirect(1)
root.attributes('-topmost',True)
root.mainloop() #this one works
screenshot of the successful one
Can someone please tell me why the sequence of those two lines matters?

tkinter messagebox always appears behind main pygame window

I am trying to make my messagebox appear in front of my pygame window, but it keeps appearing behind it. Here's my code:
from tkinter import messagebox
# pygame loop here
messagebox.showinfo("Title", "Message here")
Do I need to do add some lines of code to bring it to the front? Any help would be appreciated.
I got it to work. I had to add root.withdraw() as well.
import tkinter as tk
from tkinter import messagebox
root = tk.Tk()
root.withdraw()
# pygame loop here
messagebox.showinfo("Title", "Message here")
root.lift()
Not sure why hiding the root tkinter window makes it work...
This will put the window in the middle of the screen on the top level of everything, so it will not be hidden behind and stays in front.
window = Tk()
window.eval('tk::PlaceWindow %s center' % window.winfo_toplevel())

Categories

Resources