无法在tkinter python中看到一半的行

时间:2016-12-17 12:24:40

标签: python windows graphics tkinter

我正在创建一个游戏,我想添加一些图形。我试图创建一条线,但我只能看到它的一半。

Screenshot showing a line that only goes about halfway across the window

这是我的代码:

import Tkinter as tk
from PIL import Image, ImageTk
import time
import calculate

class Core(tk.Tk):

def run(self):
    self.mainloop()

def show_frame(self, page_name):
    '''Show a frame for the given page name'''
    frame = self.frames[page_name]
    frame.tkraise()

def __init__(self, color):
    tk.Tk.__init__(self)

    container = tk.Frame(self)
    self.geometry("600x600")
    self.configure(background=color)

    self.resizable(width=False, height=False)
    container.pack(side="top", fill="both", expand=True)


    self.iconbitmap('icon.ico')

    self.frames = {}
    for F in (Home, Game):
        page_name = F.__name__
        frame = F(parent=container, controller=self)
        self.frames[page_name] = frame
        frame.grid(row=0, column=0, sticky="nsew")


class Home(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        self.controller = controller
        label = tk.Label(self, text="This is page 1")
        label.pack(side="top", fill="x", pady=10)


class Game(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        canvas = tk.Canvas(self)
        canvas.pack(side='top', fill=tk.BOTH, expand=True)

        canvas.create_line(0, 0, 600, 600)
        #calculate.drawp(100, canvas, 600, 120, 300, 600, self)



def main(color):
    app = Core(color)
    app.show_frame("Game")
    app.run()

main("#e67e22")

我没有看到发生这种情况的原因。你能帮帮我吗?

1 个答案:

答案 0 :(得分:0)

Canvas不使用完整的窗口,因此您看不到实线。

使用

container['bg'] = 'red'

你会看到

enter image description here

您可以调整画布大小

canvas.config(width=600, height=600)

或者您可以在网格中设置最小尺寸的单元格

container.columnconfigure(0, minsize=600)
container.rowconfigure(0, minsize=600)