I'm a newbie in python-tkinter. And I have a problem that when i put widgets inside my canvas, the widgets bypasses the canvas size. But when I scroll, the canvas would not move. On top of that, the canvas auto re-sizes when I add more widgets, isn't it not suppose to re-size 'coz I already added a scrollbar?
Here's my code: -
-
from tkinter import *
-
from tkinter import ttk
-
import mysql.connector
-
-
class CanvasScroll:
-
-
def on_resize(self,event):
-
self.canvas.config(width = 1185, height = 530)
-
-
def add_crew(self):
-
crewFrame = CrewFrame()
-
-
-
def callback_list(self):
-
index = self.notebook.index(self.notebook.select()) + 1
-
self.tmpframe = Try2(self.contentframe, index)
-
-
-
def __init__(self, master):
-
self.canvas = Canvas(master, width = 1185, height = 530, scrollregion = (0, 0, 1216, 700), bg = 'white', confine = True)
-
self.canvas.grid(row = 2, column = 0, sticky = 'news')
-
-
self.xscroll = ttk.Scrollbar(master, orient = HORIZONTAL, command = self.canvas.xview)
-
self.xscroll.grid(row = 3, column = 0, sticky = 'we')
-
-
self.yscroll = ttk.Scrollbar(master, orient = VERTICAL, command = self.canvas.yview)
-
self.yscroll.grid(row = 2, column = 1, sticky = 'ns')
-
-
self.canvas.config(xscrollcommand = self.xscroll.set, yscrollcommand = self.yscroll.set)
-
self.canvas.bind("<Configure>", self.on_resize)
-
-
-
self.option = ttk.Frame(master, height = 150, width = 1206)
-
self.option.grid(row = 0, column = 0, sticky = 'we', columnspan = 5)
-
-
self.addicon = PhotoImage(file = 'C:\\Users\\rain\\Desktop\\Fyosh!\\logo\\add.gif').subsample(2,2)
-
self.btnAdd = ttk.Button(self.option, image = self.addicon, text = 'Add Crew', compound = TOP, command = self.add_crew)
-
self.btnAdd.grid(row = 0, column = 0, padx = 50, pady = 5)
-
-
self.updateicon = PhotoImage(file = 'C:\\Users\\rain\\Desktop\\Fyosh!\\logo\\update.gif').subsample(2,2)
-
self.btnUpdate = ttk.Button(self.option, image = self.updateicon, text = 'Update Crew', compound = TOP)
-
self.btnUpdate.grid(row = 0, column = 1, padx = 50)
-
-
self.deleteicon = PhotoImage(file = 'C:\\Users\\rain\\Desktop\\Fyosh!\\logo\\delete.gif').subsample(2,2)
-
self.btnDelete = ttk.Button(self.option, image = self.deleteicon, text = 'Delete Crew', compound = TOP)
-
self.btnDelete.grid(row = 0, column = 2, padx = 50)
-
-
self.reloadicon = PhotoImage(file = 'C:\\Users\\rain\\Desktop\\Fyosh!\\logo\\Refresh.png').subsample(7,7)
-
self.reloadbtn = ttk.Button(self.option, image = self.reloadicon, command = self.callback_list, text = 'Load List', compound = TOP)
-
self.reloadbtn.grid(row = 0, column = 3, padx = 50)
-
-
self.tabframe = ttk.Frame(master, height = 20)
-
self.tabframe.grid(row = 1, sticky = 'we')
-
-
self.notebook = ttk.Notebook(self.tabframe)
-
self.notebook.grid(row = 0, column = 0, sticky = 'we')
-
-
db = mysql.connector.connect(user = 'root', password = '', host = 'localhost', database = 'fat2x_payroll')
-
cursor = db.cursor()
-
-
ships = ("SELECT * from tbl_shiplist")
-
-
try:
-
cursor.execute(ships)
-
result = cursor.fetchall()
-
self.tab = {}
-
for row in result:
-
self.tab[row[0]] = {
-
"shipname": ttk.Frame(self.notebook)
-
}
-
self.notebook.add(self.tab[row[0]]["shipname"], text = row[1])
-
db.close()
-
-
except:
-
messagebox.showinfo("alert", "ship list error")
-
-
self.canvas.label = ttk.Label(self.canvas, text = "hahahahahha").grid(column = 0)
-
self.canvas.label1 = ttk.Label(self.canvas, text = "hahahahahha").grid(column = 1)
-
self.canvas.label2 = ttk.Label(self.canvas, text = "hahahahahha").grid(column = 2)
-
self.canvas.label3 = ttk.Label(self.canvas, text = "hahahahahha").grid(column = 3)
-
self.canvas.label4 = ttk.Label(self.canvas, text = "hahahahahha").grid(column = 4)
-
self.canvas.label5 = ttk.Label(self.canvas, text = "hahahahahha").grid(column = 5)
-
self.canvas.label6 = ttk.Label(self.canvas, text = "hahahahahha").grid(column = 6)
-
self.canvas.label7 = ttk.Label(self.canvas, text = "hahahahahha").grid(column = 7)
-
self.canvas.label8 = ttk.Label(self.canvas, text = "hahahahahha").grid(column = 8)
-
-
-
-
-
-
def start():
-
master = Tk()
-
master.geometry("1206x690+10+10")
-
master.resizable(0,0)
-
master.title("Crew Panel")
-
canvasScroll = CanvasScroll(master)
-
-
if __name__ == "__main__":
-
start()
-
-
Can someone please help me! :(
A mainloop statement is required to start Tkinter. Other than that, this version, with unnecessary code stripped out, works fine for me. Also take note of the master.resizable function added to the code, and remember that scrollbars only work within the scrollregion size. - class CanvasScroll:
-
def __init__(self, master):
-
## scrollregion increased but all the widgets
-
## still don't fit -- see what prints (Python 2.X)
-
self.canvas = Canvas(master, width = 1185, height = 530,
-
scrollregion = (0, 0, 1800, 700),
-
bg = 'white', confine = True)
-
self.canvas.grid(row = 2, column = 0, sticky = 'news')
-
-
self.xscroll = ttk.Scrollbar(master, orient = HORIZONTAL, command = self.canvas.xview)
-
self.xscroll.grid(row = 3, column = 0, sticky = 'we')
-
-
self.yscroll = ttk.Scrollbar(master, orient = VERTICAL, command = self.canvas.yview)
-
self.yscroll.grid(row = 2, column = 1, sticky = 'ns')
-
-
self.canvas.config(xscrollcommand = self.xscroll.set, yscrollcommand = self.yscroll.set)
-
-
for ctr in range(10):
-
self.canvas.create_text(150, 50+(ctr*100),
-
text='test-'+str(ctr+1))
-
-
## wait one second
-
master.after(1000, self.add_more)
-
-
def add_more(self):
-
scroll_width=1800
-
for ctr in range(10):
-
x=300+(ctr*200)
-
dif=scroll_width-x
-
lit=""
-
if dif < 0:
-
lit="Not Displayed"
-
print ctr+1, x, dif, lit
-
-
self.canvas.create_text(x, 50,
-
text='width-'+str(ctr+1))
-
-
master = Tk()
-
master.geometry("1206x690+10+10")
-
master.title("Crew Panel")
-
master.resizable(width=FALSE, height=FALSE)
-
canvasScroll = CanvasScroll(master)
-
master.mainloop()
1 2249
A mainloop statement is required to start Tkinter. Other than that, this version, with unnecessary code stripped out, works fine for me. Also take note of the master.resizable function added to the code, and remember that scrollbars only work within the scrollregion size. - class CanvasScroll:
-
def __init__(self, master):
-
## scrollregion increased but all the widgets
-
## still don't fit -- see what prints (Python 2.X)
-
self.canvas = Canvas(master, width = 1185, height = 530,
-
scrollregion = (0, 0, 1800, 700),
-
bg = 'white', confine = True)
-
self.canvas.grid(row = 2, column = 0, sticky = 'news')
-
-
self.xscroll = ttk.Scrollbar(master, orient = HORIZONTAL, command = self.canvas.xview)
-
self.xscroll.grid(row = 3, column = 0, sticky = 'we')
-
-
self.yscroll = ttk.Scrollbar(master, orient = VERTICAL, command = self.canvas.yview)
-
self.yscroll.grid(row = 2, column = 1, sticky = 'ns')
-
-
self.canvas.config(xscrollcommand = self.xscroll.set, yscrollcommand = self.yscroll.set)
-
-
for ctr in range(10):
-
self.canvas.create_text(150, 50+(ctr*100),
-
text='test-'+str(ctr+1))
-
-
## wait one second
-
master.after(1000, self.add_more)
-
-
def add_more(self):
-
scroll_width=1800
-
for ctr in range(10):
-
x=300+(ctr*200)
-
dif=scroll_width-x
-
lit=""
-
if dif < 0:
-
lit="Not Displayed"
-
print ctr+1, x, dif, lit
-
-
self.canvas.create_text(x, 50,
-
text='width-'+str(ctr+1))
-
-
master = Tk()
-
master.geometry("1206x690+10+10")
-
master.title("Crew Panel")
-
master.resizable(width=FALSE, height=FALSE)
-
canvasScroll = CanvasScroll(master)
-
master.mainloop()
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Dave Smithz |
last post by:
Hi,
In summary:
I want to a form to submit information via a HTTP POST, however, when using
Internet Explorer I want to be able to use the back button and all the
information retained....
|
by: Askari |
last post by:
Hi,
I do a "perso"widget where have two scrolls (one vertical, one
horizontal) for a canvas.
When, the first time, I added widget in the canvas, the scroll self-ajust
and when I move scrollbar,...
|
by: PhilC |
last post by:
Hi Folks,
I'm trying to click on a canvas to draw a line. The canvas has scroll
bars. All is well until I move the scrollbars. This naturally because
the relationship between my screen click and...
|
by: bjbounce2002 |
last post by:
Hello,
I am using forms with command buttons to close form or run action
queries. The error messages such as "Null value in required field" or
"duplicate value in primary key" are suppressed....
|
by: Solx37 |
last post by:
I am trying to figure out how to prevent scrolling when I append text.
It scrolls to the bottom whenever I use AppendText.
It jumps to the top whenever I += text to the Text property.
|
by: mikem76 |
last post by:
How do I automatically redirect stdout and stderr when using os.popen2
to start a long running process. If the process prints a lot of stuff
to stdout it will eventually stop because it runs out...
|
by: Bishman |
last post by:
Hi,
I have been trying to get text to scroll smoothly accross a windows form
using GDI+ and a timer. Changing the position of the text by a configurable
amount and calling invalidate to force a...
|
by: =?Utf-8?B?U2hhd24gU2VzbmE=?= |
last post by:
Greetings! I was researching AJAX to provide a solution to displaying status
messages while a long process executed. I found several examples online and
was able to use their code to get a quick...
|
by: rb0135 |
last post by:
Hi all (and Mark).
As I have posted a few times the last few weeks about my GPS application, I have got to the point of adding the GPS stuff in. I am using the Windows Mobile 6 SDk and used the...
|
by: Diann |
last post by:
Hello,
When using Commission Streamer software, when I click on 'use this snippet' I get the following error message.
CientUIWPFOutlookNAAFAPP1 has stopped working.
A problem caused the...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
| |