导入Tkinter和rowspan /粘性问题

时间:2018-10-26 17:17:29

标签: python tkinter sticky

我正在编写一个用于生成电子邮件草稿的程序,并且正在尝试使用tkinter GUI对其进行设置。我使用的是tkinter 8.6的Python 3.5。

我的问题是我无法使行跨度正常工作。我想让第一行跨越几行,但是当我添加“ rowspan”时,该行不会改变大小。如果我添加“ sticky = tK.N + tK.S”,则会收到错误消息,说明这些内容毫无意义。所以后来我开始弄不清楚如何导入tkinter,尝试将其作为从tkinter导入,作为tk导入,导入*,但是我所做的一切都改变了,但仍然没有得到我正在寻找的南北粘性该行将伸展并填充空间。

我很确定这是我如何导入tkinter的问题,我们将不胜感激任何建议。如果我使用“将tkinter导入为tk”,我也无法使tkinter正常工作,因为那样的话我会得到类似未定义名称“ StringVar”的错误。我尝试通过移动声明根目录的位置来修复该问题,但这会导致GUI问题。

帮助!谢谢:)

import os
# pull in GUI stuff
import tkinter as tk #import Tk, Label, Button, W, E, N, S, StringVar, OptionMenu, Entry, Text, END, WORD
from PIL import Image, ImageTk

path = os.getcwd()


# init GUI
class GUI:
    def __init__(self, master):
        self.master = master
        master.title("Email Draft Builder")
        # set logo image
        f = os.getcwd() + "\\cgs_logo.gif"
        # lock in image for use outside this section
        im = Image.open(f)
        ph = ImageTk.PhotoImage(im)
        # vars to show dynamic text for displays 1 and 2
        self.box1_titleText = tk.StringVar()
        self.box2_titleText = tk.StringVar()

        self.box1_content = tk.StringVar()
        self.box2_content = tk.StringVar()

# main/container pane info
        self.label = tk.Label(master, text="Email Draft Composer", image = ph, bg = "#ffffff")
        self.label.image = ph
        self.label.grid(columnspan = 3, row = 0, rowspan = 2, sticky=tk.N+
                        tk.S, column=1)

# ROW 1
        self.readLast_button = tk.Button(master, text="Read Training File", command=self.dataOps)
        self.readLast_button.grid(row=3, column=0,sticky=tk.W)

# ROW 2
        self.file_button = tk.Button(master, text="Unused", command=self.chooseSite)
        self.file_button.grid(row=4, column=0,sticky=tk.W)

# ROW 3
        self.pullSite_button = tk.Button(master, text="Show Site Info:", command=self.pullSite)
        self.pullSite_button.grid(row=5, column=0,sticky=tk.W)

        self.getSite = tk.Entry(master)
        self.getSite.grid(row=5, column=1,sticky=tk.W)

# ROW 4
        self.sendEmail_button = tk.Button(master, text="Build Email", command=self.sendEmail)
        self.sendEmail_button.grid(row=6, column=0,sticky=tk.W)

        recipients = ["mgancsos@cogstate.com","mgancsos@gmail.com","kkiernan@cogstate.com;mchabon@cogstate.com","ashortland@cogstate.com"]
        self.receiver = tk.StringVar()
        self.receiver.set(recipients[0])
        self.menu1 = tk.OptionMenu(master, self.receiver, *recipients)
        self.menu1.grid(row=6, column=1,sticky=tk.W)

# ROW 5
        self.close_button = tk.Button(master, text="Close", command=root.destroy)
        self.close_button.grid(row=7, column=0,sticky=tk.W)

# ROW 6
        self.box1_title = tk.Label(master, textvariable=self.box1_titleText, bg = "#fffff0", borderwidth=1, relief = "groove", width = 15)
        self.box1_title.grid(columnspan=1, row = 8, column=0, sticky=tk.W)
        self.box1_pane = tk.Label(master, textvariable=self.box1_content, bg = "#fffff0", borderwidth=1, relief = "groove", width = 55)
        self.box1_pane.grid(columnspan=1, row = 8, column=1,  sticky=tk.W)

# ROW 7
        self.box2_title = tk.Label(master, textvariable=self.box2_titleText, bg = "#ffff00", borderwidth=1, relief = "groove", width = 15)
        self.box2_title.grid(columnspan=1, row = 9, column=0, sticky='nw')
        self.box2_pane = tk.Label(master, textvariable=self.box2_content, bg = "#ffffff", borderwidth=1, relief = "groove", width = 55)
        self.box2_pane.grid(columnspan=1, row = 9, column=1, sticky='NW')
# ROW 8
        self.display1 = tk.Text(master, wrap=tk.WORD)
        self.display1.grid(columnspan=1, row = 10, column=1, sticky=tk.W)

    def dataOps(self):
        return(1)

    def chooseSite(self):
        return(1)        

    def pullSite(self):
        return(1)        

    def sendEmail(self):
        return(1)        


root = tk.Tk()
root.geometry('800x800')
root["bg"] = "#ffffff"
my_gui = GUI(root)
root.mainloop()

0 个答案:

没有答案