使用Tkinter和Python在同一个框架中将两个不同的类插入到笔记本中 [英] Problems inserting two different classes within the same frame into a notebook using Tkinter and Python

查看:688
本文介绍了使用Tkinter和Python在同一个框架中将两个不同的类插入到笔记本中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直尝试使用Tkinter和Python在同一个框架内将两个不同的类(在MyListbox和Calculation下面的代码)插入到笔记本中。实际上,我没有收到任何从我的电脑的报告,我说错了。我的电脑没有显示任何东西,甚至没有消息。你能帮我解决这个问题吗?

I have been trying inserting two different classes (in the code below "MyListbox" and "Calculation") within the same frame into a notebook using Tkinter and Python. Actually I Have not received any report from my computer saying where I am wrong. My computer doesn't show anything, not even a message. Can you help me solving this problem?

提前感谢。

这里是代码:

import Tkinter
from Tkinter import *
from Tkinter import Tk, Text, BOTH, W, N, E, S
from ttk import Frame, Button, Label, Style
import Tkinter as tk
import ttk 
import tkFont
from ttk import *
import tkMessageBox
import sys
import math
import Tkconstants 


def defocus(event):
    event.widget.master.focus_set()

root = tk.Tk()
root.title("Autana")

f= tkFont.Font(family="verdana", size=12,weight=tkFont.BOLD)#, weight=tkFont.BOLD)
f2= tkFont.Font(family="Times", size=20, weight=tkFont.BOLD)

c1= 'PeachPuff2'

notebook = ttk.Notebook(root)
notebook.pack(fill=BOTH, expand=True)
notebook.pressed_index = None

ContainerOne = Tkinter.Frame(notebook);
ContainerOne.pack(fill=BOTH, expand=True);

notebook.add(ContainerOne, text='Standard Reliability')

canvas1 = Canvas(ContainerOne, width=950, height=450,bg=c1)
scroll = Scrollbar(ContainerOne, command=canvas1.yview)
canvas1.config(yscrollcommand=scroll.set, scrollregion=(0,0,100,1000))
canvas1.pack(side=LEFT, fill=BOTH, expand=True)
scroll.pack(side=RIGHT, fill=Y)

frameOne = Tkinter.Frame(canvas1, width=900, height=450,bg=c1,bd=22)
canvas1.create_window(630, 270, window=frameOne)

class MyListbox:
    def __init__(self, parent):#, title):
        self.parent = parent
        self.myData= (
                    ["1", "Jhon Doe", "Madrid", "0341-672541", "6 SD"],
                    ["5", "Kenji S.", "Tokyo", "0341-213212", "10 SD"])
        self.establishment()

    def combobox_handler(self, event):
        current = self.combobox.current()
        self.entNumber.delete(0, END)
        self.entName.delete(0, END)
        self.entCity.delete(0, END)
        self.entTel.delete(0, END)
        self.entAddress.delete(0, END)

        self.entNumber.insert(END, self.myData[current][0])
        self.entName.insert(END, self.myData[current][1])
        self.entCity.insert(END, self.myData[current][2])
        self.entTel.insert(END, self.myData[current][3])
        self.entAddress.insert(END, self.myData[current][4])

    def establishment(self):
        mainFrame = Frame(self.parent)
        mainFrame.pack(fill=BOTH, expand=YES)

        fr_left = Frame(mainFrame)#, bd=10)
        fr_left.pack(fill=BOTH, expand=YES, side=LEFT)

        names = [person[1] for person in self.myData]
        self.combobox = ttk.Combobox(fr_left, values=names)
        self.combobox.bind('<<ComboboxSelected>>', self.combobox_handler)
        self.combobox.pack()
        self.combobox.set("Data People")

        fr_right = Frame(mainFrame)#, bd=10)
        fr_right.pack(fill=BOTH, expand=YES, side=RIGHT)

        fr_up = Frame(fr_right)
        fr_up.pack(side=TOP, expand=YES)

        Label(fr_up, text='List Number').grid(row=0, column=0, sticky=W)
        self.entNumber = Entry(fr_up)
        self.entNumber.grid(row=0, column=1)

        Label(fr_up, text='Name').grid(row=1, column=0, sticky=W)
        self.entName = Entry(fr_up)
        self.entName.grid(row=1, column=1)

        Label(fr_up, text='City').grid(row=2, column=0, sticky=W)
        self.entCity = Entry(fr_up)
        self.entCity.grid(row=2, column=1)

        Label(fr_up, text='No. Tel').grid(row=3, column=0, sticky=W)
        self.entTel = Entry(fr_up)
        self.entTel.grid(row=3, column=1)

        Label(fr_up, text='Address').grid(row=4, column=0, sticky=W)
        self.entAddress = Entry(fr_up)
        self.entAddress.grid(row=4, column=1)


class Calculation:

    def __init__(self, parent):
        self.parent = parent
        self.Value1()
        self.Value2()
        self.Result()

        Label(self.parent,text='Num 1').grid(column=2, row=5, sticky=W, pady=3)
        Label(self.parent,text='Num 2').grid(column=2, row=6, sticky=W, pady=3)
        Label(self.parent,text='result').grid(column=9,row=9, sticky=W, pady=3)

        self.msg = Label(self.parent,text='Sum of 2 number')
        self.msg.grid(row=3,column=1,columnspan=2)

        self.Button = Button(text='Calculate',width=8,command=self.Calc)
        self.Button.grid(row=9,column=2,padx=2,pady=3)

    def Value1(self):
        self.field1 = ttk.Combobox(self.parent)
        self.field1['values'] = ('5', '6', '7')
        self.field1.grid(column=3, row=5)

    def Value2(self):
        self.field2 = ttk.Combobox(self.parent)
        self.field2['values'] = ('1', '2', '3')
        self.field2.grid(column=3, row=6)

    def Result(self):
        self.entry = StringVar()
        self.entry = ttk.Entry(self.parent, textvariable=self.entry)
        self.entry.grid(column=3, row=9)

    def Calc(self):
        self.entry.delete(0, END)
        try:
            value = int(self.field1.get()) + int(self.field2.get())
        except ValueError:
            self.entry.insert(0, 'Input numbers.')
        else:
            self.entry.insert(0, str(value))

if __name__ == '__main__':

    stepOne = Tkinter.LabelFrame(frameOne, text=" 1. Select People: ",font= f2, bd=6)
    stepOne.grid(row=0, columnspan=5, sticky='nsew', \
                 padx=5, pady=5, ipadx=5, ipady=5)

    stepTwo = Tkinter.LabelFrame(frameOne, text=" 2. Calculation : ",font= f2, bd=6)
    stepTwo.grid(row=2, columnspan=7, sticky='w', \
             padx=5, pady=5, ipadx=5, ipady=5)

    app = MyListbox(stepOne)
    app2 = Calculation (stepOne)----> This is not working!
    root.mainloop()


推荐答案

使用 app2 = Calculation(stepOne),这意味着您在相同的网格位置放置具有相同父项的不同小部件。

You are using app2 = Calculation (stepOne), and that means that you are placing different widgets with the same parent in the same grid positions. The outcome in this situation is that Tkinter does not display any widget at all.

但是即使用 app2 = Calculation(stepTwo)改变它也不会显示任何窗口部件。 / code>,这两行代码仍然有同样的问题:

But even if changing it with app2 = Calculation (stepTwo), you still have the same problem with this two lines of code:

self.Button = Button(text='Calculate',width=8,command=self.Calc)
self.Button.grid(row=9,column=2,padx=2,pady=3)

您没有设置任何父级,所以它使用默认的Tk元素和我之前提到的相同的问题,但这次与根元素。

You are not setting any parent, so it uses the default Tk element and the same problem that I mentioned before occurs, but this time with the root element.

最后,我强烈建议每个模块只使用一个导入语句,因为 Tkinter ttk 对某些类使用相同的名称,如果您使用主题控件,则不同。

Finally, I strongly recommend you to use only one import statement for each module, since Tkinter and ttk uses the same names for some classes and it is different if you used a themed widget or not.

import Tkinter as tk
import ttk 
import tkFont
import tkMessageBox
import sys
import math

# ...

这篇关于使用Tkinter和Python在同一个框架中将两个不同的类插入到笔记本中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆