I'm just learning Python in school and we were suppose to draw something (code it in gedit for python) on canvas (Tkinter). Instead of getting something drawn up I only get an empty canvas. It looks like this on my computer. The code is correct as I copied it from another web page.
from Tkinter import *
master=Tk()
w=Canvas(master, width=200, height=100)
w.pack
w.create_line(0,0,200,100)
w.create_line(0,100,200,0, fill="red", dash=(4,4))
w.create_rectangle(50,25,150,75, fill="blue")
mainloop()
Actually, the code is not correct. The person who wrote it forgot to actually call the pack method. You need to add () after it to do this:
from Tkinter import *
master=Tk()
w=Canvas(master, width=200, height=100)
########
w.pack()
########
w.create_line(0,0,200,100)
w.create_line(0,100,200,0, fill="red", dash=(4,4))
w.create_rectangle(50,25,150,75, fill="blue")
mainloop()
Otherwise, the canvas will never be placed on the window.
P.S. You should note that not everything you find on the Web is guaranteed to be correct. :)
Related
I was wondering how to put a button inside of a canvas using the tkinter module. This question was asked, but it was 5 years ago and for a different version so It was not very convenient for my situation and I am still a beginner so I only understood about 3/4 of the code in the best answer. Here is the question: How to make a Button using the tkinter Canvas widget?
from tkinter import *
root = Tk()
c=Canvas(root,width=200,height=150,bg='blue')
c.pack(side = 'top')
c.create_text(100,50,anchor='c',fill='orange',font='Times 28',text='List')
button = Button(root, text="Quit",command=root.destroy)
button.pack()
mainloop()
When I run this code it creates the button below my Canvas and not on the Canvas. I looked for help on https://docs.python.org/3.7/library/tkinter.html the guide for the IDE I am using. I could not find a way to put the button on the Canvas even though I may or may not have missed something. If this question is seen as not helpful or unnecessary I apologize and will close it immediately.
Version of Python: 3.7
Level: Beginner
Running Code on: IDLE 64-bit
OS: Windows 10
When you use pack() tkinter will place the button on it's master (root), and the area where the canvas is drawn is already occupied.
To place the button on the canvas you should use the function create_window() on the canvas:
from tkinter import *
root = Tk()
c = Canvas(root,width=200,height=150,bg='blue')
c.pack(side = 'top')
c.create_text(100, 50, anchor='c', fill='orange', font='Times 28', text='List')
button = Button(root, text="Quit", command=root.destroy)
canvas_widget = c.create_window(100, 100, window=button)
root.mainloop()
delete button.pack()
try use the code below
button = Button(root, text="Quit", command=root.destroy)
c.create_window(10, 10, anchor=NW, window=button)
First of all, thank you for trying to help me out. I am currently programming my first GUI with tkinter and i try to create buttons with big fonts, cause i want to create a program for visually impaired people. Sadly I have two problems i can't solve with the internet on my own ..
This is the relevant part of my code: (sorry for the german variables)
import tkinter
from tkinter import *
from tkinter import font
import tkinter.messagebox
class Oberflaeche(tkinter.Frame):
def __init__(self, master=None):
tkinter.Frame.__init__(self, master)
self.pack()
MyFont = font.Font(family='times', size=50)
self.close_window = tkinter.Button(self, font=MyFont, text="Programm \nbeenden", command=self.close_window, bg="white", height = 3, width = 18, bd=3, relief="solid")
self.close_window.pack()
def close_window(self):
root.destroy()
root = tkinter.Tk()
root.title("Prototyp MVP")
root.minsize(width=300, height=300)
root.configure(background='white')
oberflaeche = Oberflaeche(master=root)
oberflaeche.mainloop()
when I try to change the font tkFont.Font is not working. There is the Error:
NameError: name 'tkFont' is not defined
That is the reason I tryed font.Font. But no matter how I change the font family or type, it always looks awful and pixelated....
Picture of the failed Button
I am using python 3.5.5, Ubuntu 16.04 and tk 8.6.8.
I am using Spyder(python 3.6) here is the result;
i.hizliresim.com/6NgZlW.jpg
Maybe because of version?
Actually your program is working succesfully ı did not change any codes ı tried on my pc and the result is perfect(Win10 Spyder(Python 3.6).)
i am also used to Jupyter notebook and i was trying with VS Code everything is working well. I suggest you trying to play around with scaling and let me know is it working for you?
Scaling of Tkinter GUI in 4k (3840*2160) resolution
Results
Jupyter
VS Code
I am redesigning the GUI of a program that uses tkinter in python. I used ttk widgets for this program, but I think, even on W10, the interface is way too old so I decided to update the visual interface for the program using METRO interface or W10 alike UI.
The first thing that come in mind, is that W10 have a left-side "tabs" that are very beautiful and useful, and the question is if is that a way to using the ttk.notebook widget, change the position of the tabs?
Otherwise, I could do buttons placed on the side and load frame widgets on every button clicked, but I think this could overload so much the program loading constantly frames and widgets, and I am trying to avoid this way.
Thanks to everyone.
It is possible to change the position of the tabs by configuring the tabposition option of the TNotebook style.
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
style = ttk.Style(root)
style.configure('lefttab.TNotebook', tabposition='ws')
notebook = ttk.Notebook(root, style='lefttab.TNotebook')
f1 = tk.Frame(notebook, bg='red', width=200, height=200)
f2 = tk.Frame(notebook, bg='blue', width=200, height=200)
notebook.add(f1, text='Frame 1')
notebook.add(f2, text='Frame 2')
notebook.pack()
root.mainloop()
This is how it looks like with the default theme on linux:
However, the text inside the tabs is always horizontal. I don't have Windows, so I don't know exactly how the W10 UI looks like, but I guess that you would like to rotate the tabs, not just change there position and I don't know how to do that.
I am familiarizing myself with Tkinter, and I am attempting to write a very simple program, which displays a button in a window, using the pack geometry manager.
I was experimenting with various configuration options for pack(), such as expand, fill, and side, and I've run into a peculiar problem. I have written the following code:
from Tkinter import *
root = Tk()
widget = Button(root, text='text')
widget.pack(expand=YES, fill=BOTH)
root.mainloop()
The problem is that the button expands to fill the window in the horizontal direction, but not the vertical direction. This is the same result that I get if instead of specifying fill=BOTH I use fill=X. In addition, if I specify instead fill=Y the button does not expand in either direction. Something seems to be going wrong with the fill in the vertical direction, and I cannot figure out what it might be.
I attempted to Google this problem and surprisingly found no mention of this happening to anyone else. I am using a Mac with OS X Yosemite and running python 2.7.5. I also attempted to compile with python 3.4.1 and saw no change.
Edit:
Based off of the answer and comments below, it is clear that there is nothing wrong with my code, because it seems to work on other machines. If not an error in the code, does anyone know what could possibly be causing the button to not stretch vertically when I run the above code?
This is a feature of native buttons on OSX. Buttons on OSX will be a fixed height and will not expand vertically. There is nothing you can do, short of using a different widget such as a label.
try running this code to see the behavior of fill and expand
from Tkinter import *
root = Tk()
root.geometry("500x500")
widget = Button(root, text='text1')
widget.pack(fill=X, expand=1)
widget = Button(root, text='text2')
widget.pack(fill=Y, expand=1)
widget = Button(root, text='text3')
widget.pack(fill=BOTH, expand=1)
root.mainloop()
Argument fill does fill in vertical direction as well
I am also beginner, defining geometry for fill was missing in your code as given below:
from Tkinter import *
root = Tk()
root.geometry("600x400")
widget = Button(root, text='text')
widget.pack(expand=YES, fill=BOTH)
root.mainloop()
I am trying to get a label to be center justified between a given width, but it's not working. What am I doing wrong?
from tkinter import *
from tkinter.ttk import *
def main():
root = Tk()
root.geometry("200x100")
root.minsize(0,0)
root.resizable(0,0)
a = Label(master=root, text="Hello World", justify="center", background="red")
a.pack()
a.place(x=0,y=0, width=120)
mainloop()
main()
The text is properly justified in the label. The problem is that you didn't tell the label to stretch to fill the window. To do that, pack it like this:
a.pack(fill="x")
Also, it serves no purpose to call pack and then immediately call place -- only the last one will have any effect. Plus, you should avoid using place unless you have no other choice. Place is fine, but it makes your program harder to maintain, and harder to get it to grow and shrink.